HDU 3395 Special Fish 費用流(可KM匹配)

Special Fish

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


php

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
 
題目分析:求最大費用流。拆點,對每一個 i ,連邊(s, i, 1, 0), (i + n, t, 1, 0)。當 ij = 1 時, 對 i —> j + n 連邊(i, j + n, 1, -(val[i] ^ val[j]))。因爲題目的特殊性,當最大費用出現時, 並非最大流, 而最大費用流的前提是最大流,因此再創建一些額外的邊,保證流量不破壞費用的前提下保證滿流。比較方便的是直接建邊(i, t, 1, 0)。
代碼以下:
 
#include <stdio.h>
#include <string.h>
#include <memory.h>
#define min(a, b) ((a) < (b) ? (a) : (b))
const int maxE = 100000;
const int maxN = 205;
const int oo = 0x3f3f3f3f;
struct Edge{
    int v, c, w, n;
};
Edge edge[maxE];
int adj[maxN], l;
int d[maxN], cur[maxN], a[maxN];
int inq[maxN], Q[maxE], head, tail;
int n, val[maxN];
int cost, flow, s, t;
void addedge(int u, int v, int c, int w){
    edge[l].v = v; edge[l].c = c; edge[l].w =  w; edge[l].n = adj[u]; adj[u] = l++;
    edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++;
}
int SPFA(){
    memset(d, oo, sizeof d);
    memset(inq, 0, sizeof inq);
    head = tail = 0;
    d[s] = 0;
    a[s] = oo;
    cur[s] = -1;
    Q[tail++] = s;
    while(head != tail){
        int u =  Q[head++];
        inq[u] = 0;
        for(int i = adj[u]; ~i; i = edge[i].n){
            int v = edge[i].v;
            if(!edge[i].c || d[v] <= d[u] + edge[i].w) continue;
            d[v] = d[u] + edge[i].w;
            cur[v] = i;
            a[v] = min(edge[i].c, a[u]);
            if(inq[v]) continue;
            inq[v] = 1;
            Q[tail++] = v;
        }
    }
    if(d[t] == oo) return 0;
    flow += a[t];
    cost += a[t] * d[t];
    for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){
        edge[i].c -= a[t];
        edge[i ^ 1].c += a[t];
    }
    return 1;
}
int MCMF(){
    flow = cost = 0;
    while(SPFA());
    return cost;
}
void work(){
    int x;
    while(scanf("%d", &n) && n){
        memset(adj, -1, sizeof adj);
        l = 0;
        s = 0; t = 2 * n + 1;
        for(int i = 1; i <= n; ++i){
            scanf("%d", &val[i]);
            addedge(i, t, 1, 0);
            /*
                最大費用流的前提是滿流,而因爲題意,本題會出現最大費用流並非
                滿流的狀況,因此再加幾條額外的邊保證必定滿流
            */
            addedge(i, t, 1, 0);
            addedge(i + n, t, 1, 0);
        }
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                scanf("%1d", &x);
                if(x) addedge(i, j + n, 1, -(val[i] ^ val[j]));
            }
        }
        printf("%d\n", -MCMF());
    }
}
int main(){
    work();
    return 0;
}
HDU 3955
相關文章
相關標籤/搜索