HDU 3395 Special Fish (maximum charge flow)

Source: Internet
Author: User

Special FishTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1814 Accepted Submission (s): 678


Problem Descriptionthere is a kind of special fish in the East Lake where are closed to campus of Wuhan University. It's hard to say which gender of those fish is, because every fish believes itself as a male, and it may attack one's so Me other fish are believed to be female by it.
A Fish would spawn after it had been attacked. Each fish can attack one other fish and can is only attacked once. No matter a fish is attacked or not, it can still try to attack another fish which are believed to being female by it.
There is a value we assigned to each fish and the spawns, and the fish spawned also has a value which can be calculated B Y XOR operator through the value of its parents.
We want to know the maximum possibility of the sum of the spawns.
Inputthe input consists of multiply test cases. The first line of all test case contains a integer n (0 < n <=), which is the number of the fish. The next line consists of n integers, indicating the value (0 < value <=) of each fish. The next n lines, each line contains n integers, represent a in the 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 was followed by a zero, which means the end of the input.
Outputoutput the value for each test in a single line.
Sample Input
31 2 30111011100

Sample Output
6

Author[email protected]
Sourcethe 5th guangting Cup Central China Invitational Programming Contest

Test instructions: There are n special fish, there is a value, each fish can only attack one of its trusted fish, and each fish can only be attack once, if any one fish is attack or not attack can go attack other fish. When a fish attack another fish, it lays eggs and is worth an XOR value of two fish. Ask the maximum number of eggs you can get.

Problem solving: Use the maximum cost flow. A split point. Each point has a maximum out of one degree. The value of the ovum for the value of the side.

#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; Unit price}edg[maxm];int Head[maxn],eid;int PRE[MAXN], COST[MAXN];    Point 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) {//The minimum cost cost[v] = Cost[u]+edg[i].cost in the case of an additive flow;   Pre[v]=i;                    The Edge if (!inq[v]) {if (R==MAXN) r=0 on the record path;                    Q[r++]=v;                Inq[v]=1;    }}}} return cost[enode]>=0; Judge if there is no augmented road (Judge >=0 on AC, Judge!=-inf on WA)}//is the maximum flow, the minimum cost is mincostint 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].ca            P+=1;        Mincost+=edg[i].cost; }} return ans;    int main () {//input, initialize 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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3395 Special Fish (maximum charge flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.