Special Fish
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1814 Accepted Submission(s): 678
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
[email protected]
Source
The 5th Guangting Cup Central China Invitational Programming
Contest
题意:有n条特别的鱼,都有一个价值,每条鱼只能attack一条它信任的鱼,且每条鱼只能被attack一次,如果任意一条鱼被attack或没有被attack都可以去attack其他的鱼。当一条鱼attack另一条鱼后就会产卵,价值为两条鱼价值的XOR值。问最大能得到多少卵的价值。
解题:用最大费用流。拆点。每个点最多有一个出度一个入度。卵的价值为边的价值。
#include<stdio.h> #include<string.h> #include<queue> using namespace std; const int MAXN = 10010; const int MAXM = 100100; const int INF = 1<<29; struct EDG{ int to,next,cap; int cost; //单价 }edg[MAXM]; int head[MAXN],eid; int pre[MAXN], cost[MAXN] ; //点0~(n-1) void init(){ eid=0; memset(head,-1,sizeof(head)); } void addEdg(int u,int v,int cap,int cst){ edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = cst; edg[eid].cap=cap; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst; edg[eid].cap=0; head[v]=eid++; } bool inq[MAXN]; int q[MAXN]; bool spfa(int sNode,int eNode,int n){ int l=0 , r=0; for(int i=0; i<n; i++){ inq[i]=false; cost[i]= -INF; } cost[sNode]=0; inq[sNode]=1; pre[sNode]=-1; q[r++]=sNode; while(l!=r){ int u=q[l++]; if(l==MAXN)l=0; inq[u]=0; for(int i=head[u]; i!=-1; i=edg[i].next){ int v=edg[i].to; if(edg[i].cap>0 && cost[v]<cost[u]+edg[i].cost){ //在满足可增流的情况下,最小花费 cost[v] = cost[u]+edg[i].cost; pre[v]=i; //记录路径上的边 if(!inq[v]){ if(r==MAXN)r=0; q[r++]=v; inq[v]=1; } } } } return cost[eNode]>=0; //判断有没有增广路(判断>=0就AC,判断!=-INF就WA) } //反回的是最大流,最小花费为minCost int minCost_maxFlow(int sNode,int eNode ,int& minCost,int n){ int ans=0; while(spfa(sNode,eNode,n)){ for(int i=pre[eNode]; i!=-1; i=pre[edg[i^1].to]){ edg[i].cap-=1; edg[i^1].cap+=1; minCost+=edg[i].cost; } } return ans; } int main(){ //输入,初始化init() char mapt[105][105]; int valu[105],n; while(scanf("%d",&n)>0&&n){ for(int i=1; i<=n; i++) scanf("%d",&valu[i]); for(int i=1; i<=n; i++){ scanf("%s",mapt[i]+1); } int s=0, t=n*2+1; init(); for(int i=1; i<=n; i++){ addEdg(s , i , 1 , 0); addEdg(i+n, t , 1 , 0); for(int j=1; j<=n; j++) if(mapt[i][j]=='1'&&i!=j) addEdg(i,j+n, 1 , valu[i]^valu[j]); } int maxcost=0; minCost_maxFlow(s , t , maxcost, t+1); printf("%d\n",maxcost); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。