标签:style blog http color io os ar java for
2 2 1 0 1 1 1 0 1 2 0 2 2 0 0 1 1 1 0 1 2 0
Case 1: 2 Case 2: 1
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 100010; 18 struct arc{ 19 int u,v,w,id; 20 arc(int uu = 0,int vv = 0,int ww = 0,int iid = 0){ 21 u = uu; 22 v = vv; 23 w = ww; 24 id = iid; 25 } 26 bool operator<(const arc &tmp) const{ 27 return w < tmp.w; 28 } 29 }; 30 arc e[2][maxn]; 31 int uf[maxn],n,m,k,tot1,tot2,cost; 32 int Find(int x){ 33 if(x == uf[x]) return x; 34 return uf[x] = Find(uf[x]); 35 } 36 bool uset(int u,int v){ 37 int tx = Find(u); 38 int ty = Find(v); 39 if(tx != ty) uf[tx] = ty; 40 return tx != ty; 41 } 42 bool check(int delta){ 43 int i,j,cnt; 44 for(i = 0; i <= n; ++i) uf[i] = i; 45 i = j = cnt = cost = 0; 46 arc tmp; 47 while(i < tot1 || j < tot2){ 48 if(e[0][i].w + delta <= e[1][j].w){ 49 tmp = e[0][i++]; 50 tmp.w += delta; 51 }else tmp = e[1][j++]; 52 if(uset(tmp.u,tmp.v)){ 53 cost += tmp.w; 54 if(!tmp.id) cnt++; 55 } 56 } 57 return cnt >= k; 58 } 59 int main() { 60 int u,v,w,id,cs = 1; 61 while(~scanf("%d %d %d",&n,&m,&k)){ 62 for(int i = tot1 = tot2 = 0; i < m; ++i){ 63 scanf("%d %d %d %d",&u,&v,&w,&id); 64 if(id) e[1][tot2++] = arc(u,v,w,id); 65 else e[0][tot1++] = arc(u,v,w,id); 66 } 67 e[0][tot1].w = e[1][tot2].w = INF; 68 sort(e[0],e[0]+tot1); 69 sort(e[1],e[1]+tot2); 70 int low = -100,high = 100,mid,delta; 71 while(low <= high){ 72 mid = (low + high)>>1; 73 if(check(mid)){ 74 delta = mid; 75 low = mid + 1; 76 }else high = mid - 1; 77 } 78 check(delta); 79 printf("Case %d: %d\n",cs++,cost - delta*k); 80 } 81 return 0; 82 }
标签:style blog http color io os ar java for
原文地址:http://www.cnblogs.com/crackpotisback/p/4032703.html