标签:
1 3 2 0 1 1 3 3 0 3 3 4 8 8 7 0 9 2 9 -9 -8 3 2 -9 -6 4 1 -6 -8 -5 3 3 -1 -4 -1 -6 -5 1 10 -10 7 3 -10 -3 -10 -4 -5 -2 -1 -9 1
2 2 3 0 3 1 0 3
最后所有的收益加起来减掉最小割就是最大收益。
据说上面是昂神分析的
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF = ~0U>>2; 4 const int maxn = 1024; 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] = 1; 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 a,tmp = 0; 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(low,e[i].flow)))) { 42 e[i].flow -= a; 43 e[i^1].flow += a; 44 low -= a; 45 tmp += a; 46 if(!low) break; 47 } 48 } 49 if(!tmp) d[u] = -1; 50 return tmp; 51 } 52 int dinic(int ret = 0) { 53 while(bfs()) { 54 memcpy(cur,head,sizeof cur); 55 ret += dfs(S,INF); 56 } 57 return ret; 58 } 59 int U[maxn],Th[maxn],B[maxn],L[maxn],ans[maxn],Bid[maxn],Lid[maxn]; 60 int main() { 61 int kase,n,m; 62 scanf("%d",&kase); 63 while(kase--) { 64 scanf("%d%d",&n,&m); 65 n = (1<<n); 66 m = (1<<m); 67 for(int i = tot = 0; i < n; ++i) 68 scanf("%d",Th + i); 69 for(int i = 0; i < n; ++i) 70 scanf("%d",U + i); 71 for(int i = 0; i < n; ++i) { 72 B[i] = L[i] = -1; 73 for(int j = 0,w; j < m; ++j) { 74 scanf("%d",&w); 75 w += 1024; 76 if(j >= Th[i] && B[i] < w) { 77 B[i] = w; 78 Bid[i] = j; 79 } else if(L[i] < w) { 80 L[i] = w; 81 Lid[i] = j; 82 } 83 } 84 } 85 S = n<<1; 86 T = S + 1; 87 memset(head,-1,sizeof head); 88 for(int i = 0; i < n; ++i) { 89 int k = __builtin_popcount(i); 90 add(i,i + n,INF); 91 if(k&1) { 92 add(S,i,L[i]); 93 add(i + n,T,B[i]); 94 } else { 95 add(S,i,B[i]); 96 add(i + n,T,L[i]); 97 } 98 for(int j = i + 1; j < n; ++j) { 99 if(__builtin_popcount(i^j) == 1) { 100 if(k&1) add(i,j + n,U[i]^U[j]); 101 else add(j,i + n,U[i]^U[j]); 102 } 103 } 104 } 105 dinic(); 106 for(int i = 0; i < n; ++i) { 107 if(i) putchar(‘ ‘); 108 if(__builtin_popcount(i)&1) 109 printf("%d",(~d[i])?Lid[i]:Bid[i]); 110 else printf("%d",(~d[i])?Bid[i]:Lid[i]); 111 } 112 putchar(‘\n‘); 113 } 114 return 0; 115 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4898274.html