标签:style blog http color io os java ar for
4 3 2 0 0 0 0 3 3 6 1 2 4 1 3 10 1 4 12 2 3 6 2 4 8 3 4 5 0
6
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 = 210; 18 struct arc { 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = -1) { 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 arc e[maxn*maxn]; 27 int head[maxn],d[maxn],gold[maxn],store[maxn]; 28 int tot,n,m,S,T,cur[maxn],q[maxn],hd,tl; 29 int a[maxn*maxn],b[maxn*maxn],c[maxn*maxn]; 30 void add(int u,int v,int w) { 31 e[tot] = arc(v,w,head[u]); 32 head[u] = tot++; 33 e[tot] = arc(u,0,head[v]); 34 head[v] = tot++; 35 } 36 void build(int mid) { 37 memset(head,-1,sizeof(head)); 38 tot = 0; 39 for(int i = 0; i < m; i++) 40 if(c[i] <= mid) { 41 add(a[i],b[i],INF); 42 add(b[i],a[i],INF); 43 } 44 for(int i = 1; i <= n; i++) 45 add(S,i,gold[i]); 46 for(int i = 1; i <= n; i++) 47 add(i,T,store[i]); 48 } 49 bool bfs() { 50 memset(d,-1,sizeof(d)); 51 hd = tl = 0; 52 q[tl++] = S; 53 d[S] = 1; 54 while(hd < tl) { 55 int u = q[hd++]; 56 for(int i = head[u]; ~i; i = e[i].next) { 57 if(d[e[i].to] == -1 && e[i].flow > 0) { 58 d[e[i].to] = d[u] + 1; 59 q[tl++] = e[i].to; 60 } 61 } 62 } 63 return d[T] > -1; 64 } 65 int dfs(int u,int low) { 66 if(u == T) return low; 67 int tmp = 0,a; 68 for(int &i = cur[u]; ~i; i = e[i].next) { 69 if(e[i].flow > 0 && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(low,e[i].flow)))) { 70 tmp += a; 71 low -= a; 72 e[i].flow -= a; 73 e[i^1].flow += a; 74 if(!low) break; 75 } 76 } 77 if(!tmp) d[u] = -1; 78 return tmp; 79 } 80 int dinic() { 81 int tmp = 0; 82 while(bfs()) { 83 memcpy(cur,head,sizeof(head)); 84 tmp += dfs(S,INF); 85 } 86 return tmp; 87 } 88 int main() { 89 int suma,sumb,high,low,ans; 90 while(scanf("%d",&n),n) { 91 suma = sumb = 0; 92 for(int i = 1; i <= n; i++) { 93 scanf("%d",gold+i); 94 suma += gold[i]; 95 } 96 for(int i = 1; i <= n; i++) { 97 scanf("%d",store+i); 98 sumb += store[i]; 99 } 100 scanf("%d",&m); 101 low = INF; 102 high = -1; 103 for(int i = 0; i < m; i++) { 104 scanf("%d %d %d",a+i,b+i,c+i); 105 low = min(low,c[i]); 106 high = max(high,c[i]); 107 } 108 if(suma > sumb) { 109 puts("No Solution"); 110 continue; 111 } 112 ans = -1; 113 S = 0; 114 T = n + 1; 115 while(low <= high) { 116 int mid = (low + high)>>1; 117 build(mid); 118 if(dinic() >= suma) { 119 ans = mid; 120 high = mid - 1; 121 } else low = mid + 1; 122 } 123 if(ans > 0) printf("%d\n",ans); 124 else puts("No Solution"); 125 } 126 return 0; 127 }
标签:style blog http color io os java ar for
原文地址:http://www.cnblogs.com/crackpotisback/p/3989583.html