二分多重匹配(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 ...
随机推荐
- cvWaitKey 如果 cvNamedWindow就不会起作用
Have you called cvNamedWindow yet? It will not work without cvNamedWindow. http://stackoverflow.com/ ...
- netstat 查看TCP状态值
一.TCP 状态值 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' CLOSED:无连接是活动的或正在进行LI ...
- insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a=10001
insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a ...
- 转: svn合并分支到trunk
http://sepcot.com/blog/2007/04/svn-merge-branch-trunk SVN: Merging a Branch into Trunk This is mor ...
- yum安装node.js
1.安装EPEL库 yum install epel-release 2.安装Node.js yum install nodejs 3.安装nodejs中常用的npm软件包管理器 yum instal ...
- [LeetCode] Jump Game II(贪婪算法)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【C++】动态内存与智能指针
C++常见的内存分配方式有三种: 从静态存储区分配,这里主要是存储局部static对象,类的static成员以及定义在函数之外的变量: 从栈内存分配,这里主要是存储函数内的非static对象: 从堆内 ...
- Mac废纸篓 不能完全清空的有效解决方法
摘自:http://www.neonan.com/articles/5582 对于苹果用户来说, 可能都会遇到过这样的情况(尽管这种情况是不常出现的!), Mac OS X系统突然出现一些顽固的文件家 ...
- 学习一下Fiddler的强大
①引言:Fiddler (中文名称:小提琴)是一个 HTTP 的调试代理,以代理服务器的方式,监听系统的Http网络数据流动, Fiddler 可以也可以让你检查所有的 HTTP 通讯,设置断点,以及 ...
- Xcode插件管理
在使用Xcode的时候,公司同事使用/// 和//TODO 就能打出很多注释信息.虽然他们帮忙给我也装了,但是我却不知道怎么弄的.今天在家无聊,过来自己实践了一把. so easy. 1.我使用的是P ...