直接二分白色边边权权值再做MST就可以了。。注意相同边权排序时保持黑白色边的一致性。。
/*
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y)/2
#define NM 50005
#define nm 100005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e16;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m,s,ans,p,f[NM];
struct tmp{
int x,y,v,f;
bool operator<(const tmp&o)const{return v<o.v||(v==o.v&&f<o.f);}
}a[nm];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
bool check(int t){
int cnt=0;s=0;
inc(i,1,m)if(!a[i].f)a[i].v+=t;
inc(i,1,n)f[i]=i;
sort(a+1,a+1+m);
inc(i,1,m){
int x=find(a[i].x),y=find(a[i].y);
if(x==y)continue;
f[x]=y;s+=a[i].v;
if(!a[i].f)cnt++;
}
inc(i,1,m)if(!a[i].f)a[i].v-=t;
return cnt>=p;
}
int main(){
n=read();m=read();p=read();
inc(i,1,m)a[i].x=read()+1,a[i].y=read()+1,a[i].v=read(),a[i].f=read();
for(int x=-100,y=100;x<=y;)
if(check(mid))ans=s-mid*p,x=mid+1;else y=mid-1;
return 0*printf("%d\n",ans);
}
2654: tree
Time Limit: 30 Sec Memory Limit: 512 MB
Submit: 3781 Solved: 1631
[Submit][Status][Discuss]
Description
给你一个无向带权连通图,每条边是黑色或白色。让你求一棵最小权的恰好有need条白色边的生成树。
题目保证有解。
Input
第一行V,E,need分别表示点数,边数和需要的白色边数。
接下来E行,每行s,t,c,col表示这边的端点(点从0开始标号),边权,颜色(0白色1黑色)。
Output
一行表示所求生成树的边权和。
V<=50000,E<=100000,所有数据边权为[1,100]中的正整数。
Sample Input
2 2 1
0 1 1 1
0 1 2 0
Sample Output
2
HINT
原数据出错,现已更新 by liutian,但未重测---2016.6.24
Source