tree
参考博客
题目大意
给出一个无向图,每个边有一条边权以及一种颜色(0/1),问恰好有 (k) 条颜色为 (0) 的边的最小生成树,保证有解
数据范围
(Vle50000,Ele100000)
时空限制
30sec,512MB
分析
设 (F(x)) 为包含 (x) 条 (0) 边的最小生成树大小,显然 (F(x)) 的斜率递增,而题目求最小值,符合这两个条件,那么就可以套用带权二分解决此题
注意 (kruskal) 过程中要求出答案上界,将边权相同的 (0) 边排在前面
Code
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
inline char nc() {
static char buf[100000], *l = buf, *r = buf;
return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void read(T & x) {
x = 0; int f = 1, ch = nc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
x *= f;
}
const int maxn = 50000 + 5;
const int maxm = 100000 + 5;
const int maxv = 100 + 5;
int n, m, k;
int MST, mid;
struct data {
int u, v, w, c;
bool operator < (const data & other) const {
int a = w + (c ? 0 : mid);
int b = other.w + (other.c ? 0 : mid);
if(a != b) return a < b;
return c < other.c;
}
} e[maxm];
struct union_set {
int fa[maxn];
void init() {
for(int i = 1; i <= n; ++i) fa[i] = i;
}
int find(int a) {
return a == fa[a] ? a : fa[a] = find(fa[a]);
}
bool merge(int a, int b) {
a = find(a), b = find(b);
if(a == b) return 0;
fa[a] = b; return 1;
}
} us;
bool judge() {
sort(e + 1, e + m + 1);
us.init();
int cnt = 0; MST = 0;
for(int i = 1; i <= m; ++i) {
int u = e[i].u, v = e[i].v;
if(us.merge(u, v)) {
MST += e[i].w + (e[i].c ? 0 : mid);
if(!e[i].c) cnt++;
}
}
return cnt >= k;
}
int solve() {
int l = -maxv, r = maxv, re;
while(l <= r) {
mid = (l + r) >> 1;
if(judge()) l = mid + 1, re = MST - k * mid;
else r = mid - 1;
}
return re;
}
int main() {
// freopen("testdata.in", "r", stdin);
read(n), read(m), read(k);
for(int i = 1; i <= m; ++i) {
read(e[i].u), read(e[i].v), read(e[i].w), read(e[i].c);
e[i].u++, e[i].v++;
}
printf("%d
", solve());
return 0;
}