码迷,mamicode.com
首页 > 其他好文 > 详细

hdoj 3395 Special Fish 【最大费用流】

时间:2015-08-30 06:37:25      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:



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



忍不了,找了那么长时间bug。自己还可以攻击自己——还能使自己怀孕?什么鬼?


题意:有N条很奇怪的鱼,每条鱼都有自己的价值。现在给你一个N*N的矩阵,若第i行第j列为1则表示第i条鱼认为第j条是雌性,反之不是。

这种鱼的奇怪之处——会攻击它认为是雌性的鱼,并且在攻击后,被攻击者会怀孕,而且怀孕的孩子的价值为两者价值的异或值。更奇怪的是,每条鱼最多攻击别人一次或者最多被攻击一次。问你N条鱼能够得到的最大价值。



注意:需要把每一条鱼i拆分成左点 i 和 右点i+N,注意这里不能建边,因为题目并没有说一定要被攻击一次或者攻击一次,所以我们无法确定流量。还要注意每条鱼可以选择不攻击别人。


建图:设置超级源点source,超级汇点sink。

1,source连所有鱼的左点,容量为1,费用为0;

2,所有鱼的右点连sink,容量为1,费用为0;

3,所有鱼的左点连sink,容量为1,费用为0。表示选择不攻击任何鱼;

4,所有可能的攻击关系<u, v>,建边u -> v+N,容量为1,费用为异或值。




AC代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 210
#define MAXM 100000
#define INF 0x3f3f3f3f
using 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()
{
    edgenum = 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];//每条鱼的价值
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);//不攻击直接回到汇点
    }
    char str[210];
    for(int i = 1; i <= N; i++)
    {
        scanf("%s", str);
        for(int j = 0; j < N; j++)
        {
            //if(i == j) continue; 不能加!!!
            if(str[j] == '1')
                addEdge(i, j+1+N, 1, val[i] ^ val[j+1]);//可能攻击关系建边
        }
    }
}
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.cost;
                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 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 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
 

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

hdoj 3395 Special Fish 【最大费用流】

标签:

原文地址:http://blog.csdn.net/chenzhenyu123456/article/details/48098341

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!