[BalkanOI2011] timeismoney 题解
Solution
虽然要退役了,不过还是写写题解纪念一下最后的日子吧。
可以发现的是,如果我们把 \(\sum a\) 看作 \(x\),\(\sum b\) 看作 \(y\),然后一种树就是一个点 \((x,y)\),那么答案一定在凸包上,并且是一个下凸壳,具体证明可以使用反比例函数,不过窃以为当作结论记住比较方便。
既然是在凸包上,那么 \(x\) 最小和 \(y\) 最小一定在凸包上,这两个点不妨设为 \(A,B\),那么我们可以找到距离 \(AB\) 最远的 \(C\),然后 \(C\) 一定在凸壳上,所以我们可以继续递归。考虑如何找到 \(C\),可以发现,我们只需要 \(-(AB\times AC)/2\) 最大,即是 \(AB\times AC\) 最小,展开即是 \((Bx-Ax)\times (Cy-Ay)-(By-Ay)\times (Cx-Ax)\) 最小,所以每个边边权设为 \(a\times (Bx-Ax)+b\times (Ay-By)\) 跑最小生成树即可。
复杂度不太清楚,似乎凸包上的期望点数是 \(\sqrt{\ln n}\) 的,所以我们姑且认为是 \(m\log m\times \sqrt{\ln n}\) 的吧。
Code
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define int long long
#define MAXM 10005
#define MAXN 205
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}
int n,m,fa[MAXN];
void reset (){for (Int i = 1;i <= n;++ i) fa[i] = i;}
int findSet (int x){return fa[x] == x ? x : fa[x] = findSet (fa[x]);}
struct edge{
int u,v,w,a,b;
bool operator < (const edge &p)const{return w < p.w;}
}e[MAXM];
#define pii pair<int,int>
#define se second
#define fi first
pii ans = {1e9,1e9};
pii kruskal (){
pii now = {0,0};
reset (),sort (e + 1,e + m + 1);
for (Int i = 1;i <= m;++ i){
int u = e[i].u,v = e[i].v;
if (findSet (u) == findSet (v)) continue;
fa[fa[u]] = fa[v],now.fi += e[i].a,now.se += e[i].b;
}
int vnow = now.fi * now.se,miv = ans.fi * ans.se;
if (vnow < miv || (miv == vnow && now.fi < ans.fi)) ans = now;
return now;
}
pii operator - (pii s1,pii s2){return {s1.fi - s2.fi,s1.se - s2.se};}
int operator * (pii s1,pii s2){return s1.fi * s2.se - s1.se * s2.fi;}
void solveit (pii A,pii B){
for (Int i = 1;i <= m;++ i) e[i].w = e[i].b * (B.fi - A.fi) + e[i].a * (A.se - B.se);
pii it = kruskal ();
if ((B - it) * (A - it) > 0){
solveit (A,it),solveit (it,B);
}
}
signed main(){
read (n,m);
for (Int i = 1;i <= m;++ i) read (e[i].u,e[i].v,e[i].a,e[i].b),e[i].u ++,e[i].v ++;
for (Int i = 1;i <= m;++ i) e[i].w = e[i].a;
pii A = kruskal ();
for (Int i = 1;i <= m;++ i) e[i].w = e[i].b;
pii B = kruskal ();
solveit (A,B),write (ans.fi),putchar (' '),write (ans.se),putchar ('\n');
return 0;
}