hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))

Special Fish

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


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
 
题意:现在有n条鱼,每条鱼有一个价值,然后给一个01矩阵,如果G[i][j]==1,那么第i条鱼可以攻击第j条鱼,获得的价值是V[i]XORV[j],每条鱼只能攻击和被攻击1次,问你最后获得的价值最大是多少?
题解1:很明显的二分图模型,KM算法算最优匹配即可,第二种解法才叫神...。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 999999999;
const int N = 205;
int graph[N][N];
int lx[N], ly[N];
bool visitx[N], visity[N];
int slack[N];
int match[N];
int n,m;
bool Hungary(int u)
{
    int temp;
    visitx[u] = true;
    for(int i = 1; i <= n; ++i)
    {
        if(visity[i])
            continue;
        else
        {
            temp = lx[u] + ly[i] - graph[u][i];
            if(temp == 0) //相等子图
            {
                visity[i] = true;
                if(match[i] == -1 || Hungary(match[i]))
                {
                    match[i] = u;
                    return true;
                }
            }
            else //松弛操作
                slack[i] = min(slack[i], temp);
        }
    }
    return false;
}
void KM()
{
    int temp;
    memset(match,-1,sizeof(match));
    memset(ly,0,sizeof(ly));
    memset(lx,0,sizeof(lx));
    for(int i =1;i<=n;i++)
        for(int j=1;j<= n;j++)
            lx[i] = max(lx[i], graph[i][j]);
    for(int i = 1; i <= n;i++)
    {
        for(int j = 1; j <= n;j++)
            slack[j] = INF;
        while(1)
        {
            memset(visitx,false,sizeof(visitx));
            memset(visity,false,sizeof(visity));
            if(Hungary(i))
                break;
            else
            {
                temp = INF;
                for(int j = 1; j <= n; ++j)
                    if(!visity[j]) temp = min(temp, slack[j]);
                for(int j = 1; j <= n; ++j)
                {
                    if(visitx[j]) lx[j] -= temp;
                    if(visity[j]) ly[j] += temp;
                    else slack[j] -= temp;
                }
            }
        }
    }
}
int v[N];
char mp[N][N];
int main(){
    while(scanf("%d",&n)!=EOF,n){
        for(int i=1;i<=n;i++){
            scanf("%d",&v[i]);
        }
        for(int i=1;i<=n;i++){
            scanf("%s",mp[i]+1);
            for(int j=1;j<=n;j++){
                if(mp[i][j]=='1') graph[i][j] = v[i]^v[j];
                else graph[i][j] = 0;
            }
        }
        KM();
        int ans = 0;
        for(int i=1;i<=n;i++){
            if(match[i]!=-1){
                ans+=graph[match[i]][i];
            }
        }
        printf("%d\n",ans);
    }
}

 题解2:最小费用最大流,这个题没有说要是最大流的情况下,他要的只是最小费用,而用传统的模板保证的是最大流然后再最小费用,然后这个题就被套路了.它没说每条鱼都要匹配,所以我们只要还要建一条边,让第i条鱼直接连到终点。来三张神图解释:

(这样没问题)

(1/9这条边直接被掩盖了)

(完美解决)

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 999999999;
const int N = 205;
const int M = 40005;
struct Edge{
    int u,v,cap,cost,next;
}edge[M];
int head[N],tot,low[N],pre[N];
int total ;
bool vis[N];
int flag[N][N];
void addEdge(int u,int v,int cap,int cost,int &k){
    edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
    edge[k].u=v,edge[k].v=u,edge[k].cap = 0,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
    memset(head,-1,sizeof(head));
    tot = 0;
}
bool spfa(int s,int t,int n){
    memset(vis,false,sizeof(vis));
    for(int i=0;i<=n;i++){
        low[i] = INF;
        pre[i] = -1;
    }
    queue<int> q;
    low[s] = 0;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int k=head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v;
            if(edge[k].cap>0&&low[v]>low[u]+edge[k].cost){
                low[v] = low[u] + edge[k].cost;
                pre[v] = k; ///v为终点对应的边
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1) return false;
    return true;
}
int MCMF(int s,int t,int n){
    int mincost = 0,minflow,flow=0;
     while(spfa(s,t,n))
    {
        minflow=INF+1;
        for(int i=pre[t];i!=-1;i=pre[edge[i].u])
            minflow=min(minflow,edge[i].cap);
        flow+=minflow;
        for(int i=pre[t];i!=-1;i=pre[edge[i].u])
        {
            edge[i].cap-=minflow;
            edge[i^1].cap+=minflow;
        }
        mincost+=low[t]*minflow;
    }
    total=flow;
    return mincost;
}
int n;
char mp[N][N];
int v[N];
int main(){
    while(scanf("%d",&n)!=EOF,n){
        init();
        for(int i=1;i<=n;i++){
            scanf("%d",&v[i]);
        }
        for(int i=1;i<=n;i++){
            scanf("%s",mp[i]+1);
            for(int j=1;j<=n;j++){
                if(mp[i][j]=='1') addEdge(i,j+n,1,-(v[i]^v[j]),tot);
            }
        }
        int src = 0,des = 2*n+1;
        for(int i=1;i<=n;i++){
            addEdge(src,i,1,0,tot);
            addEdge(i,des,1,0,tot); ///巧妙地一步
            addEdge(i+n,des,1,0,tot);
        }
        int ans = MCMF(src,des,2*n+2);
        printf("%d\n",-ans);
    }
}

 

 

posted @ 2016-08-03 10:11  樱花庄的龙之介大人  阅读(384)  评论(0编辑  收藏  举报