HDU3395 Special fish(费用流)

Special Fish

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1324    Accepted Submission(s): 502


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
 

Recommend
notonlysuccess

此题属于费用流裸题,建一个源点和汇点,注意一条鱼可以不与其他鱼撞,也就是说鱼可以直接连汇点。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<queue>
#define inf 0x3f
using namespace std;
struct pi
{
    int to;
    int cap;
    int cost;
    int rev;
};
vector<pi>g[205];
int dis[205];
int pre[205],pree[205];
int sink,source;
int vis[205];
int a[205];
char c[205];
void add(int a,int b,int cap,int cost)
{
    pi pp;
    pp.to=b;
    pp.cap=cap;
    pp.cost=cost;
    pp.rev=(int)g[b].size();
    g[a].push_back(pp);
    pp.to=a;
    pp.cap=0;
    pp.cost=-cost;
    pp.rev=(int)g[a].size()-1;
    g[b].push_back(pp);
    return ;
}
int spfa(void)
{
    int i,p,k;
    memset(dis,0x3f ,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    memset(pree,-1,sizeof(pree));
    dis[source]=0;
    vis[source]=1;
    queue<int>q;
    pi pp;
    q.push(source);
    while(!q.empty())
    {
        p=q.front();
        k=(int)g[p].size();
        q.pop();
        vis[p]=0;
        if(p==sink)
            continue;
        for(i=0;i<k;i++)
        {
            pp=g[p][i];
            if(pp.cap>0&&dis[pp.to]>dis[p]+pp.cost)
            {
                dis[pp.to]=dis[p]+pp.cost;
                pre[pp.to]=p;
                pree[pp.to]=i;
                if(!vis[pp.to])
                {
                    q.push(pp.to);
                    vis[pp.to]=1;
                }
            }
        }
    }
    return pre[sink]!=-1;
}
int min(int a,int b)
{
    int p;
    p=a;
    if(b<a)
        p=b;
    return p;
}
int minflow(void)
{
    int i,f;
    int res=0;
    while(spfa())
    {
        f=inf;
        for(i=sink;i!=source;i=pre[i])
        {
            f=min(f,g[pre[i]][pree[i]].cap);
        }
        res+=f*dis[sink];
        for(i=sink;i!=source;i=pre[i])
        {
            pi &e=g[pre[i]][pree[i]];
            e.cap-=f;
            g[i][e.rev].cap+=f;
        }
    }
    return res;
}
int main()
{
    int i,j,n;
    while(1)
    {
        cin>>n;
        if(!n)
            break;
        for(i=0;i<=2*n+1;i++)
        {
            g[i].clear();
        }
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            add(0,i,1,0);
            add(i,2*n+1,1,0);
        }
        for(i=1;i<=n;i++)
        {
            scanf("%s",c);
            for(j=0;j<n;j++)
            {
                if(c[j]=='1')
                {
                    add(i,n+j+1,1,-(a[i]^a[j+1]));
                }
            }
            add(i+n,2*n+1,1,0);
        }
        source=0;
        sink=2*n+1;
        printf("%d\n",-minflow());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值