题意
给一些边,每边有两种权值v1,v2(v1>v2)。分别表示一级或二级,让你求取了k条一级公路时的最小生成树的最大边权。
解析
因为边的权值范围不大,所以可以直接枚举边权,然后在边中选权值小于check值的加入。
#include <cstdio>
#include <algorithm>
#define Rep( i , _begin , _end ) for(int i=(_begin);i<=(_end);i++)
#define For( i , _begin , _end ) for(int i=(_begin);i!=(_end);i++)
using namespace std;
const int maxx = 20000 + 25;
typedef int array[maxx];
array from,to,ftr,c1,c2;
int n,m,x,y,z,k,L,R,mid,ans;
int find(int x){return ftr[x] == x? x : ftr[x] = find(ftr[x]);}
bool check(int x){
Rep( i , 1 , n ) ftr[i] = i;
int tot = 0;
For( i , 1 , m )
if(c1[i] <= x && (find(from[i]) != find(to[i])))
ftr[find(to[i])] = find(from[i]),tot ++;
if(tot < k) return 0;
For( i , 1 , m )
if(c2[i] <= x && (find(from[i]) != find(to[i])))
ftr[find(to[i])] = find(from[i]),tot ++;
return tot < n-1? 0 : 1;
}
int main(){
scanf("%d%d%d",&n,&k,&m);
For( i , 1 , m ) scanf("%d%d%d%d",&from[i],&to[i],&c1[i],&c2[i]);
L = 1 ; R = 30000 ;
while(L <= R){
mid = (L + R) >> 1;
if(check(mid)) ans = mid,R = mid - 1;
else L = mid + 1;
}
printf("%d",ans);
return 0;
}