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. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  2. MooseFS 3.0 集群环境部署过程

    1 准备好6台虚拟机:(centos7) Master server:  192.168.242.135 Cgi server:                192.168.242.135 meta ...

  3. iOS 多线程(NSThread、GCD、NSOperation)

    ios中得多线程技术主要使用3种:NSThread.NSOperation和GCD 一.NSThread: 最轻量级方法,但是不安全需要手动加锁,需要自己管理生命周期 NSThread的使用方法有2种 ...

  4. es6展开运算符

    数组的展开合并 现在有两个数组[1, 2, 3, 4]和[5, 6, 7],想要将两个函数拼接成一个新的函数. //es5的写法 let arr1 = [1, 2, 3, 4]; let arr2 = ...

  5. SummerVocation_Learning--java的String 类

    java中String属于java.lang的package包,是一个类.代表不可变的字符序列. String类的常见构造方法: String(String original),创建一个对象为orig ...

  6. Uva 派 (Pie,NWERC 2006,LA 3635)

    依然是一道二分查找 #include<iostream> #include<cstdio> #include<cmath> using namespace std; ...

  7. .NET下寄宿于控制台的HTTPS监听

    附上原文链接:https://blogs.msdn.microsoft.com/jpsanders/2009/09/29/how-to-walkthrough-using-httplistener-o ...

  8. ES6-总结

    在最近进行的项目中,已经全面使用到ES6,这里对ES6进行整理总结.用得比较多的是带*的内容,这些语法.新增类型.模块调用等从代码量上.可读性上.操作上给项目带来了不少便利.   1.语法 1.1.命 ...

  9. 基于django的个人博客网站建立(三)

    基于django的个人博客网站建立(三) 前言 网站效果可点击这里访问 今天主要完成的是文章在页面的显示以及评论,留言 具体内容 首先我希望主页面是显示我的所有文章,于是在主页面的视图函数中返回了所有 ...

  10. python字符串处理方法

    一.combine & duplicate 字符串结合和复制 字符和字符串可以用来相加来组合成一个字符串输出: 字符或字符串复制输出. 二.Extract &Slice 字符串提取和切 ...