标签:
4 2 3 10 10 6 6 6 2 0 1 2 1 2 0 1 0 1 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 1 0 1 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 1 0 0 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 0 0 1 0 0 0 0 0
Case #1: 2 Case #2: 4 Case #3: 4 Case #4: 6
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 100; 4 const int INF = 0x3f3f3f3f; 5 struct arc{ 6 int to,flow,next; 7 arc(int x = 0,int y = 0,int z = -1){ 8 to = x; 9 flow = y; 10 next = z; 11 } 12 }e[maxn*maxn]; 13 int head[maxn],d[maxn],cur[maxn],tot,S,T; 14 void add(int u,int v,int flow){ 15 e[tot] = arc(v,flow,head[u]); 16 head[u] = tot++; 17 e[tot] = arc(u,0,head[v]); 18 head[v] = tot++; 19 } 20 bool bfs(){ 21 queue<int>q; 22 memset(d,-1,sizeof d); 23 d[S] = 0; 24 q.push(S); 25 while(!q.empty()){ 26 int u = q.front(); 27 q.pop(); 28 for(int i = head[u]; ~i; i = e[i].next){ 29 if(e[i].flow && d[e[i].to] == -1){ 30 d[e[i].to] = d[u] + 1; 31 q.push(e[i].to); 32 } 33 } 34 } 35 return d[T] > -1; 36 } 37 int dfs(int u,int low){ 38 if(u == T) return low; 39 int tmp = 0,a; 40 for(int &i = cur[u]; ~i; i = e[i].next){ 41 if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(e[i].flow,low)))){ 42 e[i].flow -= a; 43 low -= a; 44 e[i^1].flow += a; 45 tmp += a; 46 if(!low) break; 47 } 48 } 49 if(!tmp) d[u] = -1; 50 return tmp; 51 } 52 int dinic(){ 53 int ret = 0; 54 while(bfs()){ 55 memcpy(cur,head,sizeof head); 56 ret += dfs(S,INF); 57 } 58 return ret; 59 } 60 int main(){ 61 int Ts,n,m,u,v,w,k,ret,cs = 1; 62 scanf("%d",&Ts); 63 while(Ts--){ 64 memset(head,-1,sizeof head); 65 scanf("%d %d",&n,&m); 66 tot = ret = S = 0; 67 T = n + m + 1; 68 for(int i = 1; i <= n; ++i){ 69 scanf("%d",&w); 70 add(S,i,w); 71 ret += w; 72 } 73 for(int i = 1; i <= m; ++i){ 74 scanf("%d",&w); 75 add(i+n,T,w); 76 } 77 for(int i = 1; i <= n; ++i){ 78 scanf("%d",&k); 79 while(k--){ 80 scanf("%d",&u); 81 add(i,u + n + 1,INF); 82 } 83 } 84 for(int i = 1; i <= m; ++i) 85 for(int j = 1; j <= m; ++j){ 86 scanf("%d",&w); 87 if(w) add(i+n,j+n,INF); 88 } 89 printf("Case #%d: %d\n",cs++,ret - dinic()); 90 } 91 return 0; 92 }
HDU 4971 A simple brute force problem.
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4471582.html