[COGS 2401]Time is Money

Posted NaVi_Awson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[COGS 2401]Time is Money相关的知识,希望对你有一定的参考价值。

Description

题库链接

给你 \(n\) 个节点 \(m\) 条边的无向连通图。每条边有两个权值 \(c,t\) ,要你生成一棵边集为 \(\mathbb{E}\) 的生成树使得 \[\sum_{e\in \mathbb{E}}e_c \times \sum_{e\in \mathbb{E}}e_t\] 最小。

\(1\leq n\leq 200,1\leq m\leq 10000\)

Solution

\(x=\sum_{e\in \mathbb{E}}e_c,y=\sum_{e\in \mathbb{E}}e_t\) 。记 \(k=xy\) ,那么对于反比例函数 \(y=\frac{k}{x}\)\(k\) 越小越贴近坐标轴。

我们将所有生成树的权值以 \((x,y)\) 的形式刻画在坐标轴上,显然满足条件的最小值一定在左下凸包上。

那么首先最极端的是单纯按 \(c\) 排序和按 \(t\) 排序得到的点 \(A,B\)

然后显然要找到在连线 \(AB\) 左下最远的点 \(C\)

由于离 \(AB\) 最远的 \(C\) 必定导致 \(S_{\Delta ABC}\) 最大。

显然就是求 \(\left(\vec{AB}\times\vec{AC}\right)_{min}\) \[\begin{aligned}\vec{AB}\times\vec{AC}&=(x_B-x_A)(y_C-y_A)-(y_B-y_A)(x_C-x_A)\\&=y_C(x_B-x_A)+x_C(y_A-y_B)+K\end{aligned}\]

其中 \(K\) 是与 \(C\) 无关的量,可以忽略。所以将所有边权改为上述式子后做一遍最小生成树就可以求出 \(C\) 了。

然后继续分治对线段 \(AC\) 和线段 \(CB\) 求解即可。递归的边界为 \(\vec{AB}\times\vec{AC}\geq 0\)

Code

//It is made by Awson on 2018.3.7
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 200, M = 10000;
void read(int &x) {
    char ch; bool flag = 0;
    for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }

int n, m, fa[N+5];
struct tt {
    int u, v, w, c, t;
    bool operator < (const tt &b) const {return w < b.w; }
}edge[M+5];
struct node {
    int x, y;
    node(int _x = 0, int _y = 0) {x = _x, y = _y; }
    int operator * (const node &b) const {return x*b.y-y*b.x; }
    node operator + (const node &b) {node t; t.x = x+b.x, t.y = y+b.y; return t; }
    node operator - (const node &b) {node t; t.x = x-b.x, t.y = y-b.y; return t; }
    void print() {write(x), putchar(' '), writeln(y); }
}ans, A, B;
int find(int x) {return fa[x] ? fa[x] = find(fa[x]) : x; }

void update(node &ans, node x) {if (1ll*ans.x*ans.y > 1ll*x.x*x.y || (1ll*ans.x*ans.y == 1ll*x.x*x.y && ans.x > x.x)) ans = x; }
bool judge(node A, node B, node C) {return (B-A)*(C-A) >= 0; }
node kruskal() {
    memset(fa, 0, sizeof(fa)); int cnt = 0; node tmp;
    sort(edge+1, edge+1+m);
    for (int i = 1; i <= m; i++) {
    if (find(edge[i].u)^find(edge[i].v)) {
        ++cnt; fa[find(edge[i].u)] = find(edge[i].v);
        tmp = tmp+node(edge[i].c, edge[i].t);
        if (cnt == n-1) break;
    }
    }
    return tmp;
}
void doit(node A, node B) {
    int x = B.x-A.x, y = A.y-B.y; node C;
    for (int i = 1; i <= m; i++) edge[i].w = x*edge[i].t+y*edge[i].c;
    update(ans, C = kruskal());
    if (judge(A, B, C)) return;
    doit(A, C), doit(C, B);
}
void work() {
    read(n), read(m);
    for (int i = 1; i <= m; i++) read(edge[i].u), read(edge[i].v), read(edge[i].c), read(edge[i].t), ++edge[i].u, ++edge[i].v;
    for (int i = 1; i <= m; i++) edge[i].w = edge[i].c;
    A = ans = kruskal();
    for (int i = 1; i <= m; i++) edge[i].w = edge[i].t;
    update(ans, B = kruskal());
    doit(A, B);
    ans.print();
}
int main() {
    work(); return 0;
}

以上是关于[COGS 2401]Time is Money的主要内容,如果未能解决你的问题,请参考以下文章

坑The server time zone value '?й???????' is unrecognized or represents more than one time z(代

CSUFT 1004 This is Halloween: Saving Money

How the blockchain is changing money and business

QM1_Time value of Money

科技区块链如何改变货币和商业 | How the blockchain is changing money and biz

翻译如何输掉时间和金钱 How To Lose Time And Money