Special Fish

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14 Accepted Submission(s): 9
 
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
 
Source
The 5th Guangting Cup Central China Invitational Programming Contest
 
Recommend
notonlysuccess
 
/*
题意:有n条鱼,每条鱼最多只能攻击一次,被攻击一次,一次攻击产生的价值是val[i]^val[j],现在问你在满足条件的情况下能获得的最大价值
是多少。 #初步思路:简化成最小割最大团问题,将0点设置成源点,tol+1设置成汇点,可以想到将一个点拆成两个点,i和i+n分别表示攻击边和被攻击边
然后跑一下 #感悟:刚换了乌版图系统用的调了几天,终于正常的写代码哈哈哈。
*/
#include<bits/stdc++.h>
using namespace std;
int val[];//用来存储每个鱼的价值
int n;
char mapn[][];//用来存储攻击关系
int tol=;//要是总的可能的点数
/****************************************最小割最大流模板****************************************/
#define INF 1e9
using namespace std;
const int maxn=+; struct Edge
{
int from,to,cap,flow,cost;
Edge(){}
Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
}; struct MCMF
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n,int s,int t)
{
this->n=n, this->s=s, this->t=t;
edges.clear();
for(int i=;i<n;++i) G[i].clear();
} void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back(Edge(from,to,cap,,cost));
edges.push_back(Edge(to,from,,,-cost));
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BellmanFord(int &flow, int &cost)
{
for(int i=;i<n;++i) d[i]=INF;
memset(inq,,sizeof(inq));
d[s]=, a[s]=INF, inq[s]=true, p[s]=;
queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front(); Q.pop();
inq[u]=false;
for(int i=;i<G[u].size();++i)
{
Edge &e=edges[G[u][i]];
if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
{
d[e.to]= d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]= min(a[u],e.cap-e.flow);
if(!inq[e.to]){ Q.push(e.to); inq[e.to]=true; }
}
}
}
if(d[t]==INF) return false;
flow +=a[t];
cost +=a[t]*d[t];
int u=t;
while(u!=s)
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -=a[t];
u = edges[p[u]].from;
}
return true;
} int Min_cost()
{
int flow=,cost=;
while(BellmanFord(flow,cost));
//cout<<flow<<" "<<cost<<endl;
return cost;
}
}MM;
/****************************************最小割最大流模板****************************************/
void build(){//建边
tol=n*+;//总的可能的点数
MM.init(tol+,,tol);
//cout<<n<<endl;
//cout<<tol<<endl;
for(int i=;i<n;i++){
MM.AddEdge(,i+,,);
MM.AddEdge(i+,tol,,);
MM.AddEdge(i++n,tol,,);
for(int j=;j<n;j++){
if(mapn[i][j]==''){//这两个能打起来
MM.AddEdge(i+,j+n+,,-(val[i]^val[j]));
//cout<<-(val[i]^val[j])<<endl;
}
//cout<<j<<endl;
}
} }
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<n;i++){
scanf("%d",&val[i]);
} for(int i=;i<n;i++){
scanf("%s",mapn[i]);
}
//for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// cout<<mapn[i][j]<<" ";
// }
// cout<<endl;
//}
build();
//cout<<"ok"<<endl;
printf("%d\n",-MM.Min_cost()); }
return ;
}

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(拆点+最大费用最大流)

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

  3. HDU3395 Special Fish(最大费用任意流)

    题目要的并不是最大匹配下得到的最大的结果. 网上流行的做法是加边.其实,在连续增广的时候求得一个可行流的总费用为负就停止这样就行了. #include<cstdio> #include&l ...

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

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

  5. 【转】KM匹配题集

    转自:http://blog.csdn.net/shahdza/article/details/7779324 [HDU]2255 奔小康赚大钱 模板题★1533 Going Home 模板题★242 ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  8. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

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

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

随机推荐

  1. ng-model值字符串转数值型(convertToNumber directive)

    <select ng-model="model.id" convert-to-number> <option value="0">Zer ...

  2. Bootstrap框架的了解和使用之栅格系统

       前    言 Bootstrap Bootstrap 包含了一个响应式的.移动设备优先的.不固定的网格系统,可以随着设备或视口大小的增加而适当地扩展到 12 列.它包含了用于简单的布局选项的预定 ...

  3. 【个人笔记】《知了堂》MySQL中的数据类型

    MySQL中的数据类型 1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节  范围(-128~127) smallint(m) 2个字节  范围(-32768~32767) ...

  4. ES6 Promise 对象

    Promise 的含义 Promise 是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供了Pro ...

  5. jmeter测试教程

    http://www.cnblogs.com/TankXiao/p/4045439.html

  6. UI自动化测试简介及Selenium工具的介绍和环境搭建

    自动化测试简介 1.1何为自动化测试? 是把以人为驱动的测试转化为机器执行的一种过程,它是一种以程序测试程序的过程.换言之,就是以程序实现的方式来代替手工测试. 1.2自动化测试分类 分为功能自动化测 ...

  7. MySQL之多表操作

    前言:之前已经针对数据库的单表查询进行了详细的介绍:MySQL之增删改查,然而实际开发中业务逻辑较为复杂,需要对多张表进行操作,现在对多表操作进行介绍. 前提:为方便后面的操作,我们首先创建一个数据库 ...

  8. numpy学习整理

    今天先整理到这里,剩下的下次再整理 1.改变形状: reshape()返回改变的数组形状,但无法改变源数组形状 resize() 可以改变源数组形状 ravel() 输出类似C数组的列表,和resha ...

  9. 初学者易上手的SSH-struts2 01环境搭建

    首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...

  10. python之testcenter操作

    一.设置python环境 1. 从以下路径中将StcPython.py文件拷贝出来 Linux: /Installdir/Spirent_TestCenter_4.xx/Spirent_TestCen ...