HDU_3395_Special Fish(最大费用最大流)

Special Fish

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



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
 
题意:有n条鱼,每条鱼最多可攻击其他鱼一次,被别人攻击一次。题目给出一个n*n的矩阵,如果matrix[i][j] == 0,表示 i 不攻击 j;如果matrix[i][j] == 1,表示 i 可以攻击 j,并且费用为value[i] ^ value[j]。现在问在满足条件的前提下获得的最大费用。

分析:最大费用最大流。首先应该想到的是,把每条鱼 i 拆成两个点,一个代表攻击(i),一个代表被攻击(i + n)。然后该题目求得应该是最大费用任意流,并非最大流。要转化成最小费用最大流来做,首先把费用变成负值,然后必须添加一些边,来保证在满流的前提下得到最小费用。如何加边,请看建图过程。

建图:
加入超级源点 s(0),使得 s 指向每条鱼,连边 s -> i,容量为1,费用为0;
加入超级汇点 t (2 * n + 1),使得每条鱼指向 t,连边 i + n -> t,容量为1,费用为0;
对于鱼 i 可攻击鱼 j,连边 i -> j + n,容量为1,费用为 -(value[i] ^ value[j]);
加入额外边,使得所有鱼指向汇点 t,i -> t,容量为1,费用为0。
 
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3395

代码清单:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

#define end() return 0

typedef long long ll;   
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxn = 100 + 5;
const int INF = 0x7f7f7f7f;

struct Edge{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};

struct MCMF{
    int n,m,flow,cost;
    vector<Edge>edge; //边数的两倍
    vector<int>G[2*maxn]; //邻接表,G[i][j]表示i的第j条边在e数组中的序号
    int inq[2*maxn]; //是否在队列
    int d[2*maxn]; //Bellman-Ford
    int p[2*maxn]; //上一条弧
    int a[2*maxn]; //可改进量

    void init(int n){
        this -> n = n;
        for(int i=0;i<=n;i++) G[i].clear();
        edge.clear();
    }

    void addEdge(int from,int to,int cap,int cost){
        edge.push_back(Edge(from,to,cap,0,cost));
        edge.push_back(Edge(to,from,0,0,-cost));
        m=edge.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BellmanFord(int s,int t,int& flow,int& cost){
        memset(d,INF,sizeof(d));
        memset(inq,0,sizeof(inq));
        d[s]=0; inq[s]=1; p[s]=0; a[s]=INF;

        queue<int>q;
        q.push(s);
        while(!q.empty()){
            int u=q.front();q.pop();
            inq[u]=0;
            for(int i=0;i<G[u].size();i++){
                Edge& e=edge[G[u][i]];
                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){
                        q.push(e.to);
                        inq[e.to]=1;
                    }
                }
            }
        }

        if(d[t]==INF) return false;
        flow+=a[t];
        cost+=d[t]*a[t];
        for(int u=t;u!=s;u=edge[p[u]].from){
            edge[p[u]].flow+=a[t];
            edge[p[u]^1].flow-=a[t];
        }
        return true;
    }

    //需要保证初始网络中没有负权圈
    void MincostMaxflow(int s,int t){
        flow=0,cost=0;
        while(BellmanFord(s,t,flow,cost));
    }
};


int N,tail;
int value[maxn];
char matrix[maxn][maxn];
MCMF mcmf;

void input(){
    for(int i=0;i<N;i++){
        scanf("%d",&value[i]);
    }
    for(int i=0;i<N;i++){
        scanf("%s",matrix[i]);
    }
}

void createGraph(){
    tail=2*N+1;
    mcmf.init(tail+1);
    for(int i=0;i<N;i++){
        mcmf.addEdge(0,i+1,1,0);
        mcmf.addEdge(i+1,tail,1,0);
        mcmf.addEdge(i+1+N,tail,1,0);
        for(int j=0;j<N;j++){
            if(matrix[i][j]=='1'){
                mcmf.addEdge(i+1,j+1+N,1,-(value[i]^value[j]));
            }
        }
    }
}

void solve(){
    createGraph();
    mcmf.MincostMaxflow(0,tail);
    printf("%d\n",-mcmf.cost);
}

int main(){
    while(scanf("%d",&N)!=EOF&&N){
        input();
        solve();
    }end();
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值