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

解题思路:

题意为有n条特殊的鱼,每一个鱼都有一个价值,假设鱼i ”觉得“ 鱼j 性别不同,那么就攻击它。生殖的后代的价值为 v[i] ^ v[j], 每条鱼仅仅能攻击或被攻击一次,问最后生殖的后代的最大价值为多少。

也是比較裸的二分图最大权不匹配,边i,j的权值等于 v[i] ^ v[j] 。

http://blog.csdn.net/sr_19930829/article/details/40650359

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn=102;
const int inf=0x3f3f3f;
int nx,ny;//左右两边的点数
int v[maxn];//每条鱼的value
int g[maxn][maxn];//邻接矩阵
int linked[maxn];//右边的点和左边哪个点连接
int lx[maxn],ly[maxn];//左右点的标号
int slack[maxn];//slack[j]表示右边的点j的全部不在导出子图的边相应的lx[i]+ly[j]-w[i][j]的最小值
bool visx[maxn],visy[maxn]; bool DFS(int x)//hungary求增广路
{
visx[x]=true;
for(int y=0;y<ny;y++)
{
if(visy[y])
continue;
int tmp=lx[x]+ly[y]-g[x][y];
if(tmp==0)
{
visy[y]=true;
if(linked[y]==-1||DFS(linked[y]))
{
linked[y]=x;
return true;
}
}
else if(slack[y]>tmp)
slack[y]=tmp;
}
return false;
} int KM()
{
memset(linked,-1,sizeof(linked));
memset(ly,0,sizeof(ly));
for(int i=0;i<nx;i++)
{
lx[i]=-inf;
for(int j=0;j<ny;j++)
if(g[i][j]>lx[i])
lx[i]=g[i][j];
}
for(int x=0;x<nx;x++)
{
for(int y=0;y<ny;y++)
slack[y]=inf;
while(true)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(DFS(x))
break;
int d=inf;
for(int y=0;y<ny;y++)
if(!visy[y]&&d>slack[y])
d=slack[y];
for(int i=0;i<nx;i++)
if(visx[i])
lx[i]-=d;
for(int i=0;i<ny;i++)
{
if(visy[i])
ly[i]+=d;
else
slack[i]-=d;
}
}
}
int ans=0;
for(int y=0;y<ny;y++)
{
if(linked[y]!=-1)
ans+=g[linked[y]][y];
}
return ans;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n)
{
nx=ny=n;
for(int i=0;i<n;i++)
scanf("%d",&v[i]);
int ch;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%1d",&ch);//输入格式1d
if(ch==1)
g[i][j]=v[i]^v[j];
else
g[i][j]=0;
}
}
printf("%d\n",KM());
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

[ACM] HDU 3395 Special Fish (最大重量二分图匹配,KM算法)的更多相关文章

  1. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

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

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

  3. 牛客多校第五场 E room 二分图匹配 KM算法模板

    链接:https://www.nowcoder.com/acm/contest/143/E来源:牛客网 Nowcoder University has 4n students and n dormit ...

  4. 二分图匹配--KM算法

    Kuhn-Munkres算法 KM算法,求完备匹配下的最大权匹配,时间复杂度O(\(n^3\)) 所谓的完备匹配就是在二部图中,x点集中的所有点都有对应的匹配 且 y点集中所有的点都有对应的匹配 ,则 ...

  5. ACM学习历程—POJ3565 Ants(最佳匹配KM算法)

    Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees ...

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

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

  7. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

随机推荐

  1. 【前段开发】10步掌握CSS定位: position static relative absolute float

    希望能帮到须要的人,转自:http://www.see-design.com.tw/i/css_position.html 1. position:static 元素的 position 屬性默認值為 ...

  2. 数据挖掘 决策树算法 ID3 通俗演绎

    决策树是对数据进行分类,以此达到预測的目的.该决策树方法先依据训练集数据形成决策树,假设该树不能对全部对象给出正确的分类,那么选择一些例外添�到训练集数据中,反复该过程一直到形成正确的决策集.决策树代 ...

  3. POJ 3414 Pots 记录路径的广搜

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  4. sql优化-提防错误关联

    在写sql时,在多表关联时,有时候容易把关联关系写错.一般情况下,该问题比较容易发现,但如果sql较长时,光靠眼力就比较难发现了.今天写了一个脚本,碰到该问题了. 第一版本的脚本如下: select ...

  5. umlの实现图

    在uml中大部分模型描写叙述了逻辑和设计方面的信息: 用例图知道期望 类图能够知道问题域的词汇(类.对象) 状态图.交互图和活动图能够知道类图中的词汇是怎样写作完毕行为的(逻辑结构) 实现图是用来描写 ...

  6. poj1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 题意:有N个点,每两个点至多只有一条边,如果在一个结点上放一个士兵,那他能看守与之相连的边,问最少放多少个兵,才能把所有的边能看 ...

  7. Catch Up 朋友小聚 - 地道英语 - BBC Learning English BBC英语教学 - 爱思英语网

    Catch Up 朋友小聚 - 地道英语 - BBC Learning English BBC英语教学 - 爱思英语网 Catch Up 朋友小聚 分享到: 新浪微博 QQ空间 腾讯微博 微信 更多 ...

  8. JSP内置对象之request

    书接上回,上次跟大家概括的说了说JSP的九种常用内置对象.接下来就该聊聊它们各自的特点了,今天先说说request吧. 下面是request的一些常用方法: isUserInRole(String r ...

  9. cocos2dx-lua牧场小游戏(一)

    环境: cocos2dx-3.0rc2,   xcode5.0 一.lua项目建立參考 http://blog.csdn.net/daydayup_chf/article/details/249641 ...

  10. linuxserver启动过程

    随着Linux的应用日益广泛.特别是在网络应用方面,有大量的网络server使用Linux操作系统.因为Linux的桌面应用和Windows相比另一 定的差距.所以在企业应用中往往是Linux和Win ...