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

题目链接:HDU 3395

这题跟卖啤酒那差不多,一条鱼只能攻击一次也只能被攻击一次,因此把鱼拆成攻击点1~n和被攻击点n+1~2*n,然后加入源汇点连边即可,但是最大的值不一定是最大流的情况下出现的,因此要在找dis[T]大于等于0的时候停止寻找即可。这题还有一个最大的坑!负号-和异或号^的优先级不一样,记得加括号…………

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=110;
const int M=N+N+N*N;
struct edge
{
int to,nxt,cap,cost;
edge(){}
edge(int _to,int _nxt,int _cap,int _cost):to(_to),nxt(_nxt),cap(_cap),cost(_cost){}
};
edge E[M<<1];
int head[N<<1],tot;
int dis[N<<1],pre[N<<1],path[N<<1];
bitset<N<<1>vis;
char Mat[N][N];
int mc,mf,val[N]; void init()
{
CLR(head,-1);
tot=0;
mc=mf=0;
}
inline void add(int s,int t,int cap,int cost)
{
E[tot]=edge(t,head[s],cap,cost);
head[s]=tot++;
E[tot]=edge(s,head[t],0,-cost);
head[t]=tot++;
}
int SPFA(int s,int t)
{
CLR(dis,INF);
vis.reset();
queue<int>Q;
dis[s]=0;
vis[s]=1;
Q.push(s);
while (!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=0;
for (int i=head[u]; ~i; i=E[i].nxt)
{
int v=E[i].to;
if(dis[v]>dis[u]+E[i].cost&&E[i].cap>0)
{
dis[v]=dis[u]+E[i].cost;
pre[v]=u;
path[v]=i;
if(!vis[v])
{
vis[v]=1;
Q.push(v);
}
}
}
}
return dis[t]<0;
}
void MCMF(int s,int t)
{
while (SPFA(s,t))
{
int Mf=INF;
for (int i=t; i!=s; i=pre[i])
Mf=min(Mf,E[path[i]].cap);
for (int i=t; i!=s; i=pre[i])
{
E[path[i]].cap-=Mf;
E[path[i]^1].cap+=Mf;
}
mf+=Mf;
mc+=Mf*dis[t];
}
}
int main(void)
{
int n,i,j;
while (~scanf("%d",&n)&&n)
{
init();
for (i=1; i<=n; ++i)
scanf("%d",&val[i]);
int S=0,T=n+n+1;
for (i=1; i<=n; ++i)
{
scanf("%s",Mat[i]+1);
add(S,i,1,0);//n
add(i+n,T,1,0);//n
for (j=1; j<=n; ++j)
{
if(Mat[i][j]=='1')
add(i,n+j,1,-(val[i]^val[j]));//n*n
}
}
MCMF(S,T);
printf("%d\n",-mc);
}
return 0;
}

HDU 3395 Special Fish(拆点+最大费用最大流)的更多相关文章

  1. [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 ...

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

    求最大费用能够将边权取负以转化成求最小费用. 然而此时依旧不正确.由于会优先寻找最大流.可是答案并不一定出如今满流的时候.所以要加一些边(下图中的红边)使其在答案出现时满流. 设全部边的流量为1,花费 ...

  3. UVA 1658 海军上将(拆点法+最小费用限制流)

    海军上将 紫书P375 这题我觉得有2个难点: 一是拆点,要有足够的想法才能把这题用网络流建模,并且知道如何拆点. 二是最小费用限制流,最小费用最大流我们都会,但如果限制流必须为一个值呢?比如这题限制 ...

  4. Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负

    /** 题目:Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负 链接:https://vjudge.net/problem/UVA-1161 ...

  5. LOJ 2321 清华集训2017 无限之环 拆点+最小费用最大流

    题面:中文题面,这里不占用篇幅 分析: 看到题面,我就想弃疗…… 但是作为任务题单,还是抄了题解…… 大概就是将每个格子拆点,拆成五个点,上下左右的触点和一个负责连源汇点的点(以下简称本点). 这个这 ...

  6. hdu 2485 Destroying the bus stations 最小费用最大流

    题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...

  7. poj3422 拆点法x->x'建立两条边+最小费用最大流

    /** 题目:poj3422 拆点法+最小费用最大流 链接:http://poj.org/problem?id=3422 题意:给定n*n的矩阵,含有元素值,初始sum=0.每次从最左上角开始出发,每 ...

  8. 【bzoj2661】[BeiJing wc2012]连连看 最大费用最大流

    题目描述 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭区间[a,b]中的全部整数,如果其中某两个数x,y(设x>y ...

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

    Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. 【51nod1677】treecnt(树上数学题)

    点此看题面 大致题意: 给你一个节点从1~n编号的树,让你从中选择k个节点并通过选择的边联通,且要使选择的边数最少,让你计算对于所有选择k个节点的情况最小选择边数的总和. 题解 这道题乍一看很麻烦:最 ...

  2. Jmeter命令行参数

    一.在linux中,使用非gui的方式执行jmeter.若需更改参数,必须先编辑jmx文件,找到对应的变量进行修改,比较麻烦.因此,可以参数化一些常用的变量,直接在Jmeter命令行进行设置 二.参数 ...

  3. char的有无符号类型

    char 分为有符号性(signed)和无符号型(unsigned)两种: Ø若是signed型,就意味着取值范围为[-128,127]: Ø若是unsigned型,就意味着取值范围为[0,255]: ...

  4. Hadoop集群批量命令执行

    ./pdsh -R ssh -w node-10-0[0-5] hostname -R:指定传输方式,默认为rsh,本例为ssh,如果希望ssh传输需要另行安装pdsh-rcmd-ssh,如果希望ss ...

  5. 微信小程序页面跳转绑定点击事件

    https://www.cnblogs.com/mrszhou/p/7931747.html

  6. 4396: [Usaco2015 dec]High Card Wins

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 275  Solved: 175[Submit][Status][Discuss] Descriptio ...

  7. PAT 乙级 1078 / 1084

    题目 PAT 乙级 1078 PAT 乙级 1084 题解 1078和1084这两道题放在一块写,主要是因为这两道题的解法和做题思路非常相似:之前我做这一类题没有一个固定的套路,想到哪写到哪,在某种程 ...

  8. 【计数】cf938E. Max History

    发现有一种奇怪的方法不能快速预处理? 复习一下常见的凑组合数的套路 You are given an array a of length n. We define fa the following w ...

  9. 安全 - 堡垒机 - Jumpserver

    GitHub - jumpserver/Dockerfile: Jumpserver all in one Dockerfile https://github.com/jumpserver/Docke ...

  10. java模式及其应用场景

    最经典的java 23种设计模式及具体例子(转发) 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容 ...