hdu 3395 Special Fish(异或,最大费用任意流,最优匹配)
来源:互联网 发布:luajit for windows 编辑:程序博客网 时间:2024/05/01 23:14
Special Fish
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2193 Accepted Submission(s): 827
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.
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.
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
现在给你每只鱼的价值,卵的价值是父母的价值的异或值,求最大的卵价值之和
思路:很明显的费用流,也很简单。 不过此题有一个坑点,题目没说每条鱼都要匹配,那么当出现这种情况时,就不是最大匹配了,我们要的是最优匹配,所以1~n都往终点连一条费用为0的边即可
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;#define N 1050#define INF 0x3f3f3f3fstruct Edge{ int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用} edge[N*N];int cnt,head[N];int vis[N],d[N],pp[N];char ma[110][110],v[N];int sumflow;///最大流量总和void init(){ cnt=0; memset(head,-1,sizeof(head));}void addedge(int from,int to,int cap,int cost){ edge[cnt].from=from; edge[cnt].to=to; edge[cnt].cost=cost; edge[cnt].cap=cap; edge[cnt].next=head[from]; head[from]=cnt++; edge[cnt].from=to; edge[cnt].to=from; edge[cnt].cost=-cost; edge[cnt].cap=0; edge[cnt].next=head[to]; head[to]=cnt++;///存反向边}int spfa(int s,int t,int n){ queue<int>q; memset(vis,0,sizeof(vis)); memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号 for(int i=0; i<=n; i++) d[i]=-INF; d[s]=0; vis[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].to; if(edge[i].cap>0&&d[v]<d[u]+edge[i].cost) { d[v]=d[u]+edge[i].cost; pp[v]=i; if(!vis[v]) { vis[v]=1; q.push(v); } } } } if(d[t]==-INF) return 0;///找不到一条到终点的路 return 1;}int MCMF(int s,int t,int n){ int mincost=0,minflow,flow=0;///最小费用,路径中最小流量,总流量 while(spfa(s,t,n))///找当前的最短路 { minflow=INF+1; for(int i=pp[t]; i!=-1; i=pp[edge[i].from]) minflow=min(minflow,edge[i].cap);///从路径中找最小的流量 flow+=minflow;///总流量加上最小流量 for(int i=pp[t]; i!=-1; i=pp[edge[i].from]) { edge[i].cap-=minflow;///当前边减去最小流量 edge[i^1].cap+=minflow;///反向边加上最小流量 } mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次) } sumflow=flow; return mincost;}int main(){ int n; while(~scanf("%d",&n)&&n) { init(); for(int i=1;i<=n;i++) scanf("%d",&v[i]); for(int i=1;i<=n;i++) scanf("%s",ma[i]+1); int s=0,t=2*n+1; for(int i=1;i<=n;i++) { addedge(s,i,1,0); addedge(i,t,1,0); } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i!=j&&ma[i][j]=='1') addedge(i,n+j,1,v[i]^v[j]); for(int i=n+1;i<=2*n;i++) addedge(i,t,1,0); printf("%d\n",MCMF(s,t,t)); } return 0;}
0 0
- hdu 3395 Special Fish(异或,最大费用任意流,最优匹配)
- HDU 3395 Special Fish(最大费用流)
- hdu 3395 Special Fish 最大费用流
- hdu 3395 Special Fish【最大费用流】
- 【HDU】 3395 Special Fish 费用流(可KM匹配)
- hdu 3395 Special Fish(费用流//KM匹配)
- hdu 3395 Special Fish 带权匹配 费用流
- HDU 3395Special Fish 最“大”费用最大流
- hdoj 3395 Special Fish 【最大费用流】
- hdu 3395 Special Fish(费用流)
- HDU 3395 Special Fish | 费用流
- HDU 3395 Special Fish(费用流)
- HDU 3395 Special Fish 费用流
- HDU 3395 Special Fish(二分图中最优匹配)
- HDU 3395 Special Fish(二分图中最优匹配)
- HDU 3995 Special Fish(KM最大匹配)
- HDU 3395 Special Fish 二分匹配(求最大匹配值)
- HDU 3395 Special Fish?费用流(虚拟边)?647★KM(加虚拟边权)
- csapp读书笔记-signal处理(阻塞、排队、中断)
- AC自动机模板
- CDH安装概览篇
- leetcode 92. Reverse Linked List II 链表 部分反转
- cubemx+stm32f302 驱动新西达30A电调
- hdu 3395 Special Fish(异或,最大费用任意流,最优匹配)
- Android异步消息处理机制的源码分析
- 快慢指针
- ubuntu+cuda安装问题总结
- No manual entry for pthread_create
- UE4联网Steam
- 并查集 POJ 2236
- MYSQL left join 优化与解决办法
- SD卡操作之一