HDU 3395 Special Fish(最大费用流)

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-21 11:35:49

HDU 3395 Special Fish(最大费用流)的相关文章

HDU 3395 Special Fish 最“大”费用最大流

求最大费用可以将边权取负以转化成求最小费用.然而此时依然不对,因为会优先寻找最大流,但是答案并不一定出现在满流的时候.所以要加一些边(下图中的红边)使其在答案出现时满流.设所有边的流量为1,花费如下图所示.显然最大花费是1001,而没有红边的情况下会得到3. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio>

HDU 3395 Special Fish(拆点+最大费用最大流)

Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2367    Accepted Submission(s): 878 Problem Description There is a kind of special fish in the East Lake where is closed to campus o

HDU 3395 Special Fish(费用流)

题目地址:HDU 3395 刷了几道白书和CF上的非算法的智商题,感觉智商越来越接近负数了...还是先刷几道简单题缓缓.. 这题很简单,二分图模型,用费用流也可以,用KM也可以.不过需要注意的是这里是最大费用流,并不是最大费用最大流,区别在于是否是最大流,这题可以不是最大流,所以要当费用开始减少的时候停止继续流,来保证费用是最大的. 代码如下: #include <iostream> #include <cstdio> #include <string> #includ

[ACM] HDU 3395 Special Fish (二分图最大权匹配,KM算法)

Special Fish 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 s

HDU 3395 Special Fish【最大权匹配】

题意:最大权匹配 分析:最大全匹配 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 105; 7 const int INF = 2000000000; 8 9 bool Sx[maxn], Sy[maxn]; 10 int Lx[maxn], Ly[maxn]; 11 int Link[maxn]

hdu 4862 Jump 上下界费用流

对于每个点拆点成为两个点a,b,连接a到b的上界为1,下界为1的边,保证用过一次且仅一次. 然后若点u可到达点v,则连接即可.建成了一个上下界网络,将下界拆出去,求最大费用最大流就好. #include <stdio.h> #include <iostream> #include <string.h> using namespace std; const int N=800; const int MAXE=200000; const int inf=1<<3

HDU 3667 Transportation(网络流之费用流)

题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k,需要让每一次增广到的流量都是1,这就需要把每一条边的流量都是1才行.但是每条边的流量并不是1,该怎么办呢.这个时候可以拆边,反正c最多只有5,拆成5条流量为1的边.但是这时候费用怎么办呢,毕竟平方的关系不能简单把每一条边加起来.这时候可以把拆的边的流量设为1,3,5,7,9.如果经过了3个流量,那就肯定会流1,3,5,费用为9,是3的平方,同理,其他的也是如此.然后按照给出的边建图跑一次费用流就可以了. 代码如下: #i

hdoj 3395 Special Fish 【最大费用流】

Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1920    Accepted Submission(s): 717 Problem Description There is a kind of special fish in the East Lake where is closed to campus o

HDU 2686 &amp;&amp; HDU 3376(网络流之费用流)

题目地址:HDU 2686       HDU 3376 这两道题目除了数据大小外是一样的.前者只有30*30,但是后者却成了600*600..本来以为前者代码用到后者会超时,迟迟没敢交,但是感觉能用费用流的话也只能这么做了,于是改了改数组大小就交上去了.还真没超时.. 这题又是一道关于来回最短路的.最大费用可以把费用改成相反数,最后再转成相反数就是最大费用了. 建图思路是拆点,限制每个点只能经过一次.然后将每个点与右边的和下边的连边.源点与汇点要设为2个流量. 不知道为什么用G++叫就一直WA