RT,不知为什么这个代码过不了。。
#include<bits/stdc++.h>
using namespace std;
const int E=1e5+10,V=5e4+10;
int v,e,need;
struct Node
{
int s,t,c,col;
}w[E],b[E],edge[E];
int f[V];
int ans;
int blacksum,whitesum;
int read()
{
int x=0,f=1;
char s=getchar();
while(s<'0'||s>'9')
{
if(s=='-')
f=-f;
s=getchar();
}
while(s>='0'&&s<='9')
{
x=x*10+s-48;
s=getchar();
}
return x*f;
}
bool cmp(Node i,Node j)
{
return i.c<j.c;
}
void modify(int x)//类似于归并
{
int i=1,j=1,k=1;
while(i<=whitesum&&j<=blacksum)
{
if(w[i].c<=b[j].c+x)
edge[k++]=w[i++];
else edge[k++]=b[j++];
}
while(i<=whitesum) edge[k++]=w[i++];
while(j<=blacksum) edge[k++]=b[j++];
}
int get(int x)
{
return f[x]==x?x:f[x]=get(f[x]);
}
int check(int X)
{
int res=0,cnt=0;
for(int i=1;i<=v;i++) f[i]=i;
for(int i=1;i<=e;i++)
{
int x=get(edge[i].s),y=get(edge[i].t);
if(x==y) continue;
f[x]=y;
if(edge[i].col==0) cnt++;
res+=edge[i].c;
}
if(cnt>=need) ans=res;//这里不减去消耗是因为前面赋值的时候就没有算消耗
return cnt;
}
int main()
{
int s,t,c,col;
v=read(),e=read(),need=read();
for(int i=1;i<=e;i++)
{
s=read(),t=read(),c=read(),col=read();
s++,t++;
if(col==0)
{
whitesum++;//白边分类
w[whitesum].s=s,w[whitesum].t=t,w[whitesum].c=c,w[whitesum].col=0;
}
else
{
blacksum++;//黑边分类
b[blacksum].s=s,b[blacksum].t=t,b[blacksum].c=c,b[blacksum].col=1;
}
}
sort(b+1,b+blacksum+1,cmp);
sort(w+1,w+whitesum+1,cmp);//排序
int l=-100,r=100,mid;
while(l<=r)
{
mid=l+r>>1;
modify(mid);//修改
if(check(mid)>=need)//判断
r=mid-1;
else l=mid+1;
}
printf("%d",ans);
return 0;
}