# 代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N=50010,M=100010;
int n,m,t,l,r,mid,ans,father[N];

struct edge
{
int u,v,dis,col;
}e[M];

bool cmp(edge x,edge y)
{
if (x.dis<y.dis) return 1;
if (x.dis>y.dis) return 0;
return x.col<y.col;
}

int find(int x)
{
return x==father[x]?x:father[x]=find(father[x]);
}

int kruskal()
{
for (int i=1;i<=n;i++)
father[i]=i;
int cnt=0,sum=0;
for (int i=1;i<=m;i++)
{
int x=find(e[i].u),y=find(e[i].v);
if (x!=y)
{
if (!e[i].col) cnt++;
father[x]=y;
sum+=e[i].dis;
}
}
if (cnt>=t) ans=sum-mid*t;
return cnt;
}

int main()
{
scanf("%d%d%d",&n,&m,&t);
for (int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].dis,&e[i].col);
e[i].u++; e[i].v++;
}
l=-100; r=100;
while (l<=r)
{
mid=(l+r)>>1;
for (int i=1;i<=m;i++)
if (!e[i].col) e[i].dis+=mid;
sort(e+1,e+1+m,cmp);
if (kruskal()>=t) l=mid+1;
else r=mid-1;
for (int i=1;i<=m;i++)
if (!e[i].col) e[i].dis-=mid;
}
printf("%d",ans);
return 0;
}

posted @ 2020-04-03 21:08  stoorz  阅读(...)  评论(...编辑  收藏