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
31 2 30111011100
Sample Output
6
Author
momodi@whu
Source
The 5th Guangting Cup Central China Invitational Programming Contest
题目分析:求最大费用流。拆点,对每个 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 <stdio.h>
#include <string.h>
#include <memory.h>
#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;
}