博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3395 Special Fish 费用流(可KM匹配)
阅读量:6830 次
发布时间:2019-06-26

本文共 3906 字,大约阅读时间需要 13 分钟。

Special Fish

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1339    Accepted Submission(s): 509

Problem Description
There is a kind of special fish in the East Lake where is closed to campus of Wuhan University. It’s hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of some other fish who is believed to be female by it.
A fish will spawn after it has been attacked. Each fish can attack one other fish and can only be attacked once. No matter a fish is attacked or not, it can still try to attack another fish which is believed to be female by it.
There is a value we assigned to each fish and the spawns that two fish spawned also have a value which can be calculated by XOR operator through the value of its parents.
We want to know the maximum possibility of the sum of the spawns.
 

 

Input
The input consists of multiply test cases. The first line of each test case contains an integer n (0 < n <= 100), which is the number of the fish. The next line consists of n integers, indicating the value (0 < value <= 100) of each fish. The next n lines, each line contains n integers, represent a 01 matrix. The i-th fish believes the j-th fish is female if and only if the value in row i and column j if 1.
The last test case is followed by a zero, which means the end of the input.
 

 

Output
Output the value for each test in a single line.
 

 

Sample Input
3 1 2 3 011 101 110 0
 

 

Sample Output
6
 

 

Author
momodi@whu
 

 

Source
 
题目分析:求最大费用流。拆点,对每个 i ,连边(s, i, 1, 0), (i + n, t, 1, 0)。当 ij = 1 时, 对 i —> j + n 连边(i, j + n, 1, -(val[i] ^ val[j]))。由于题目的特殊性,当最大费用出现时, 并不是最大流, 而最大费用流的前提是最大流,所以再建立一些额外的边,保证流量不破坏费用的前提下保证满流。比较方便的是直接建边(i, t, 1, 0)。
代码如下:
 
#include 
#include
#include
#define min(a, b) ((a) < (b) ? (a) : (b))const int maxE = 100000;const int maxN = 205;const int oo = 0x3f3f3f3f;struct Edge{ int v, c, w, n;};Edge edge[maxE];int adj[maxN], l;int d[maxN], cur[maxN], a[maxN];int inq[maxN], Q[maxE], head, tail;int n, val[maxN];int cost, flow, s, t;void addedge(int u, int v, int c, int w){ edge[l].v = v; edge[l].c = c; edge[l].w = w; edge[l].n = adj[u]; adj[u] = l++; edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++;}int SPFA(){ memset(d, oo, sizeof d); memset(inq, 0, sizeof inq); head = tail = 0; d[s] = 0; a[s] = oo; cur[s] = -1; Q[tail++] = s; while(head != tail){ int u = Q[head++]; inq[u] = 0; for(int i = adj[u]; ~i; i = edge[i].n){ int v = edge[i].v; if(!edge[i].c || d[v] <= d[u] + edge[i].w) continue; d[v] = d[u] + edge[i].w; cur[v] = i; a[v] = min(edge[i].c, a[u]); if(inq[v]) continue; inq[v] = 1; Q[tail++] = v; } } if(d[t] == oo) return 0; flow += a[t]; cost += a[t] * d[t]; for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){ edge[i].c -= a[t]; edge[i ^ 1].c += a[t]; } return 1;}int MCMF(){ flow = cost = 0; while(SPFA()); return cost;}void work(){ int x; while(scanf("%d", &n) && n){ memset(adj, -1, sizeof adj); l = 0; s = 0; t = 2 * n + 1; for(int i = 1; i <= n; ++i){ scanf("%d", &val[i]); addedge(i, t, 1, 0); /* 最大费用流的前提是满流,而由于题意,本题会出现最大费用流并不是 满流的情况,所以再加几条额外的边保证一定满流 */ addedge(i, t, 1, 0); addedge(i + n, t, 1, 0); } for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ scanf("%1d", &x); if(x) addedge(i, j + n, 1, -(val[i] ^ val[j])); } } printf("%d\n", -MCMF()); }}int main(){ work(); return 0;}
HDU 3955

 

转载于:https://www.cnblogs.com/ac-luna/p/3757446.html

你可能感兴趣的文章
col-md-*和col-sm-*
查看>>
前端开发大众手册(包括工具、网址、经验等)
查看>>
IOC容器
查看>>
“利益相关者”课堂讨论电子版
查看>>
意见总结
查看>>
servlet增删改查
查看>>
php - 中文字符串分割
查看>>
图解HTTP
查看>>
HTML - form (转)
查看>>
浅析C#深拷贝与浅拷贝 (转)
查看>>
3226. [SDOI2008]校门外的区间【线段树】
查看>>
如何解决jersey框架中以json格式返回数组,当数组中元素一个时json格式不对
查看>>
HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
查看>>
Kafka参数调优实战,看这篇文章就够了!
查看>>
delphi 把一个表的内容转到另一个表暂存时出错的解决方法。
查看>>
JavaScript 操作cookie
查看>>
BeanUtils.copyProperties() 用法
查看>>
微信公众平台开发 - 基础篇
查看>>
WinForm更新文件
查看>>
setprecision **fixed
查看>>