题目大意:n个点m条边的无向图,每条边有权值c和v,选出一个生成树,最小化 ∑c∗∑v
题解:膜题解
我的收获:数形结合套路
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std;
int n,m,f[205];
struct edge
{
int x,y,t,c;
LL v;
}e[100005];
struct Point
{
LL x,y;
}ans,A,B;
bool cmp(edge a,edge b)
{
return a.v<b.v;
}
int Getfather(int x)
{
return f[x]==x?x:f[x]=Getfather(f[x]);
}
Point Kruscal()
{
Point p=(Point){0,0};
sort(e+1,e+1+m,cmp);
for (int i=1;i<=n;i++)
f[i]=i;
int now=1;
for (int i=1;i<=m;i++)
{
int fx=Getfather(e[i].x),fy=Getfather(e[i].y);
if (fx==fy) continue;
p.x+=e[i].c,p.y+=e[i].t;
f[fx]=fy;
now++;
if (now==n) break;
}
if ((ans.x*ans.y==p.x*p.y&&p.x<ans.x)||ans.x*ans.y>p.x*p.y)
ans=p;
return p;
}
LL Cross(Point a,Point b,Point c)
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
void Solve(Point a,Point b)
{
LL y=a.y-b.y,x=b.x-a.x;
for (int i=1;i<=m;i++)
e[i].v=1LL*e[i].c*y+1LL*e[i].t*x;
Point p=Kruscal();
if (Cross(p,a,b)>=0) return;
Solve(a,p);
Solve(p,b);
}
int main()
{
ans.x=1e9,ans.y=1e9;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
scanf("%d%d%d%d",&e[i].x,&e[i].y,&e[i].c,&e[i].t),
e[i].x++,e[i].y++,e[i].v=e[i].c;
A=Kruscal();
for (int i=1;i<=m;i++)
e[i].v=e[i].t;
B=Kruscal();
Solve(A,B);
printf("%lld %lld\n",ans.x,ans.y);
return 0;
}