Hdoj 3395 Special Fish "Maximum cost flow"

Source: Internet
Author: User



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


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



Can't endure, looking for such a long time bug. You can also attack yourself-can you make yourself pregnant? What the heck?


Test instructions: There are n very strange fish, every fish has its own value. Now give you a n*n matrix, if line I of J is listed as 1, it means that article I fish think that article J is a female, and vice versa.

The strange thing about this fish is that it will attack the fish it considers to be female, and after the attack, the attacker will be pregnant, and the value of the child being pregnant is different or worth the value of the two. Even more strangely, each fish is attacked at most once or at most. Ask you the maximum value that n fish can get.



Note: You need split each fish I into the left point I and the right point i+n, note that here can not build edge, because the topic does not say must be attacked once or attack once, so we can not determine the traffic. Also note that each fish can choose not to attack others.


Build: Set Super source point sources, super sink point sink.

1,source the left point of all fish, the capacity is 1, the cost is 0;

2, the right point of all fish sink, capacity of 1, the cost is 0;

3, the left point of all fish is connected to the sink, the capacity is 1, the cost is 0. Indicates the choice not to attack any fish;

4, all possible attack relations <u, V>, built-in U-and v+n, capacity 1, cost is different or value.




AC Code:


#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 210# Define MAXM 100000#define INF 0x3f3f3f3fusing namespace std;struct edge{int from, to, cap, flow, cost, next;}; Edge Edge[maxm];int HEAD[MAXN], Edgenum;int PRE[MAXN], Dist[maxn];bool vis[maxn];int n;int source, sink;void Init () {ED    Genum = 0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v, int w, int c) {Edge E1 = {u, V, W, 0, C, Head[u]};    Edge[edgenum] = E1;    Head[u] = edgenum++;    Edge E2 = {V, u, 0, 0,-C, Head[v]};    Edge[edgenum] = E2; HEAD[V] = edgenum++;}    int val[maxn];//value of each fish void Getmap () {Source = 0, sink = n*2+1;        for (int i = 1; I <= N; i++) {scanf ("%d", &val[i]);        Addedge (source, I, 1, 0);        Addedge (I+n, sink, 1, 0);    Addedge (i, sink, 1, 0);//Do not attack direct return to meeting point} char str[210];        for (int i = 1; I <= N; i++) {scanf ("%s", str); for (int j = 0; J < N; J + +) {//if (i = = j) Continue;            Can't Add!!!     if (str[j] = = ' 1 ') Addedge (i, J+1+n, 1, val[i] ^ val[j+1]);//May attack relationship built-in}}}bool SPFA (int s, int t) {    Queue<int> Q;    memset (Dist,-inf, sizeof (Dist));    Memset (Vis, false, sizeof (VIS));    memset (Pre,-1, sizeof (pre));    Dist[s] = 0;    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();        Vis[u] = false;            for (int i = head[u]; i =-1; i = Edge[i].next) {Edge E = Edge[i]; if (Dist[e.to] < Dist[u] + e.cost && e.cap > E.flow) {dist[e.to] = Dist[u] + e.cos                T                Pre[e.to] = i;                    if (!vis[e.to]) {vis[e.to] = true;                Q.push (e.to); }}}} ' return pre[t]! =-1;}    void MCMF (int s, int t, int &cost) {cost = 0;        while (SPFA (s, t)) {int Min = INF; for (int i = Pre[t]; I! =-1;            i = Pre[edge[i^1].to]) {Edge E = Edge[i];        min = min (min, e.cap-e.flow);            } for (int i = pre[t]; i =-1; i = pre[edge[i^1].to]) {edge[i].flow + = Min;            Edge[i^1].flow = Min;        Cost + = Edge[i].cost * Min;        }}}int Main () {while (scanf ("%d", &n), N) {init ();        Getmap ();        int cost;        MCMF (source, sink, cost);    printf ("%d\n", cost); } return 0;}




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


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

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

Hdoj 3395 Special Fish "Maximum cost 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.