hdu 3395 Special Fish(异或,最大费用任意流,最优匹配)

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.
 

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

题意:有n只鱼,每只鱼都认为自己是男的,然后认为另外一些鱼是女的,他们会相互攻击女性鱼,一只鱼只能攻击一次并且只能被攻击一次,被攻击后会产下卵

现在给你每只鱼的价值,卵的价值是父母的价值的异或值,求最大的卵价值之和

思路:很明显的费用流,也很简单。  不过此题有一个坑点,题目没说每条鱼都要匹配,那么当出现这种情况时,就不是最大匹配了,我们要的是最优匹配,所以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 0x3f3f3f3f
struct 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值