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
0 1 1 1
0 1 2 0
Sample Output
2
HINT
原数据出错,现已更新 by liutian,但未重测---2016.6.24
正解:二分答案+最小生成树
解题报告:
今天LCF的分治题出了这道农boy题,完全不知道跟分治有什么关系。。。
题解太神,想通这么搞的目的之后就很简单了。
首先如果直接做,显然用的白边不一定是要求的值,如果边数多了,那么我们考虑如果所有的白边都增加一个值,显然白边数不会增加,应该会减少。这样就可以使我们确定答案选取了哪些白边。边数少了的话,也是一个道理。所以我们考虑二分答案,增加减少的边权必须在边权范围内(多了没意义)。
注意一下细节,相等情况下,先选白边,可以证明更优。我是蒟蒻,我不会证。
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
int n,m,allow;
int first[MAXN];
int father[MAXN];
int ecnt,cnt;
LL tot;
LL ans; struct edge{
int u,v,w;
int flag;
}e[MAXM]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline int find(int x){
if(father[x]!=x) father[x]=find(father[x]);
return father[x];
} inline bool cmp(edge q,edge qq){ if(q.w==qq.w) return q.flag<qq.flag; return q.w<qq.w; } inline bool check(int x){
for(int i=;i<=n;i++) father[i]=i;
for(int i=;i<=m;i++) if(!e[i].flag) e[i].w+=x;
sort(e+,e+m+,cmp);
int r1,r2; cnt=; tot=;
for(int i=;i<=m;i++) {
r1=find(e[i].u); r2=find(e[i].v);
if(r1!=r2) {
father[r2]=r1;
if(!e[i].flag) cnt++;
tot+=e[i].w;
}
}
for(int i=;i<=m;i++) if(!e[i].flag) e[i].w-=x;
if(cnt>=allow) return true;
return false;
} inline void work(){
n=getint(); m=getint(); allow=getint();
for(int i=;i<=m;i++) {
e[i].u=getint(); e[i].v=getint(); e[i].w=getint(); e[i].flag=getint();
}
int l=-,r=,mid;
while(l<=r) {
mid=(l+r)/;
if(check(mid)) {
l=mid+; ans=tot-mid*allow;
}else{
r=mid-;
}
}
printf(OT,ans);
} int main()
{
work();
return ;
}