二分多重匹配(HDU5093)
Battle ships
Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable
and incontrollable.
But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.
The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:
A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg
Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’,
‘o’, that symbolize iceberg, ordinary sea and floating ice.
2
4 4
*ooo
o###
**#*
ooo*
4 4
#***
*#**
**#*
ooo#
3
5
#****
*#**#
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"map"
#define inf 0x3f3f3f3f
#define M 3009
using namespace std;
char mp[55][55];
int row[55],col[55];
int l[M],r[M];
int G[3000][3000],x[3000],y[3000],use[3000];
int finde(int u,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(!use[i]&&G[u][i])
{
use[i]=1;
if(y[i]==0||finde(y[i],n))
{
y[i]=u;
x[u]=i;
return 1;
}
}
}
return 0;
}
int max_match(int cnt1,int cnt2)
{
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
int ans=0;
for(int i=1;i<=cnt1;i++)
{
if(x[i]==0)
{
memset(use,0,sizeof(use));
ans+=finde(i,cnt2);
}
}
return ans;
}
int num[88],vis[66][66];
struct node
{
int v;
node(int vv)
{
v=vv;
}
};
vector<node>edge[M];
int used[66];
int main()
{
int T,m,n,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%s",mp[i]);
int cnt1=0;
for(i=0;i<n;i++)
{
int flag=0;
row[i]=0;
for(j=0;j<m;j++)
{
if(mp[i][j]=='*')
{
if(!flag)
{
row[i]++;
l[++cnt1]=i;
flag=1;
}
edge[cnt1].push_back(j);//记录当前行和一个连续的*的列号
}
if(mp[i][j]=='#')
flag=0;
}
}
int cnt2=0;
for(j=0;j<m;j++)
{
col[j]=0;
int flag=0;
for(i=0;i<n;i++)
{
if(mp[i][j]=='*')
{
if(!flag)
{
col[j]++;
r[++cnt2]=j;
flag=1;
}
vis[i][j]=cnt2;//记录第i行第j列应与右边相连的点的编号
}
if(mp[i][j]=='#')
flag=0;
}
}
memset(G,0,sizeof(G));
for(i=1;i<=cnt1;i++)
{
memset(used,0,sizeof(used));
for(j=0;j<(int)edge[i].size();j++)
used[edge[i][j].v]=1;
for(j=1;j<=cnt2;j++)
{
if(mp[l[i]][r[j]]=='*'&&used[r[j]])
{
G[i][vis[l[i]][r[j]]]=1;
}
}
}
int ans=max_match(cnt1,cnt2);
printf("%d\n",ans);
for(i=1;i<=cnt1;i++)
edge[i].clear();
}
return 0;
}
二分多重匹配(HDU5093)的更多相关文章
- hdu 1669(二分+多重匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 思路:由于要求minimize the size of the largest group,由此 ...
- poj 2112(二分+多重匹配)
题目链接:http://poj.org/problem?id=2112 思路:由于要求奶牛走的最远距离的最短路程,显然我们可以二分距离,如果奶牛与挤奶器的距离小于等于limit的情况下,能够满足,则在 ...
- poj-2289.jamies contact groups(二分答案 + 二分多重匹配)
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9227 Accepted: ...
- [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)
http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...
- 稳定的奶牛分配 && 二分图多重匹配+二分答案
题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- POJ 2289 Jamie's Contact Groups(多重匹配+二分)
题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是 ...
- POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)
解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪.每头猪对每一个猪圈有一个惬意值.要求安排这 ...
- POJ 2289(多重匹配+二分)
POJ 2289(多重匹配+二分) 把n个人,分到m个组中.题目给出每一个人可以被分到的那些组.要求分配完毕后,最大的那一个组的人数最小. 用二分查找来枚举. #include<iostream ...
随机推荐
- Lazarus中Base64的操作
在字符串处理中,我们经常需要对文件编码然后再进行传输,通常会使用base64编码,在FreePascal中默认集成了这个单元,我们来介绍如何使用: 首先需要在引用单元的时候使用: use base64 ...
- php安装,mysql安装
先安装mysql 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 选择“Source Code”,用已经注册好的oracle账 ...
- 数据库主键跟外键+修改mysql的密码
update myspl.user set password=PASSWORD(设置的密码) where user='root'; 如果修改错误:先执行use mysple;再重复上面的代码. 一. ...
- 【python PIL学习】给照片打水印
不知道别人是怎么做的,闲着无聊就根据前几天看到的一些PIL里面的函数,写了这个小程序,经过测试,基本可以给所有像宽400以上的图片打上从左下到右上的看上去质量尚可的水印,更小的图片水印偏下.写完觉得很 ...
- ORA-01000:超出打开游标的最大数(C#)
在做一个windows服务,通过查询文本不断的插入数据的功能.测试一直没有问题,到实际环境中跑起来后程序退出,查看日志发现报的这个错误 ORA-01000:超出打开游标的最大数 经过上网查询发现是由于 ...
- qt QString 与 int,char的转换
每次QString转换int或者char的时候都要查资料,记录一下,方便下次查看. 参考: http://blog.csdn.net/ei__nino/article/details/7297791 ...
- ASIHTTPRequest 记录过去5秒的平均流量字节/秒
//记录过去5秒的平均流量字节/秒 NSLog(@"%llu",[ASIHTTPRequest averageBandwidthUsedPerSecond]);
- java获取当前时间戳的方法
获取当前时间戳 //方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); //方法 三 n ...
- 用java给php写个万能接口
package helloworld; import java.io.IOException; import javax.servlet.ServletException; import javax. ...
- PHP for 循环
<!DOCTYPE html> <html> <body> <?php for ($x=0; $x<=10; $x++) { echo "数字 ...