hdoj 3395 Special Fish 【最大费用流】

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

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

时间: 2024-10-21 11:35:46

hdoj 3395 Special Fish 【最大费用流】的相关文章

HDU 3395 Special Fish(最大费用流)

Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1814    Accepted Submission(s): 678 Problem Description There is a kind of special fish in the East Lake where is closed to campus o

HDU 3395 Special Fish 最“大”费用最大流

求最大费用可以将边权取负以转化成求最小费用.然而此时依然不对,因为会优先寻找最大流,但是答案并不一定出现在满流的时候.所以要加一些边(下图中的红边)使其在答案出现时满流.设所有边的流量为1,花费如下图所示.显然最大花费是1001,而没有红边的情况下会得到3. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio>

HDU 3395 Special Fish(拆点+最大费用最大流)

Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2367    Accepted Submission(s): 878 Problem Description There is a kind of special fish in the East Lake where is closed to campus o

HDU 3395 Special Fish(费用流)

题目地址:HDU 3395 刷了几道白书和CF上的非算法的智商题,感觉智商越来越接近负数了...还是先刷几道简单题缓缓.. 这题很简单,二分图模型,用费用流也可以,用KM也可以.不过需要注意的是这里是最大费用流,并不是最大费用最大流,区别在于是否是最大流,这题可以不是最大流,所以要当费用开始减少的时候停止继续流,来保证费用是最大的. 代码如下: #include <iostream> #include <cstdio> #include <string> #includ

[ACM] HDU 3395 Special Fish (二分图最大权匹配,KM算法)

Special Fish 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 s

HDU 3395 Special Fish【最大权匹配】

题意:最大权匹配 分析:最大全匹配 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 105; 7 const int INF = 2000000000; 8 9 bool Sx[maxn], Sy[maxn]; 10 int Lx[maxn], Ly[maxn]; 11 int Link[maxn]

Cyclic Tour HDUOJ 费用流

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 1399    Accepted Submission(s): 712 Problem Description There are N cities in our country, and M one-way roads connecting them. Now Li

hdoj 3488 Tour 【最小费用最大流】【KM算法】

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2299    Accepted Submission(s): 1151 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 3000

hdoj 2282 Chocolate 【最小费用最大流】

Chocolate Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 565    Accepted Submission(s): 281 Problem Description Lethe loves eating chocolates very much. In Lethe's birthday, her good friend ec