二分多重匹配(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 ...
随机推荐
- 各大网站CSS代码初始化集合
css代码之所以初始化,是因为能尽量减少 各浏览器之间的兼容性问题! 腾讯QQ官网 样式初始化 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fiel ...
- XPS to Blender 2.7x
XPS to Blender 2.7x(Blender internal the easy way) Things we are gonna need are Blender 2.7x www.ble ...
- docker learning
Docker 配置文件位置 Docker 的配置文件可以设置大部分的后台进程参数,在各个操作系统中的存放位置不一致 在 ubuntu 中的位置是:/etc/default/docker 在 cento ...
- Python的深拷贝与浅拷贝
一.前奏:熟悉Python内存管理 在Python中,变量在第一次赋值时自动声明,在创建—也就是赋值的时候,解释器会根据语法和右侧的操作数来决定新对象的类型. 引用计数器:一个内部跟踪变量 引用计数: ...
- netstat 查看TCP状态值
一.TCP 状态值 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' CLOSED:无连接是活动的或正在进行LI ...
- Dlib is a modern C++ toolkit(非常全面的类库)
http://dlib.net/ http://download.csdn.net/detail/lajuedan2508/9726225 http://download.csdn.net/detai ...
- 【Android开发学习笔记】【第三课】Activity和Intent
首先来看一个Activity当中启动另一个Activity,直接上代码说吧: (1)首先要多个Activity,那么首先在res-layout下新建一个 Other.xml,用来充当第二个Activi ...
- 1058 N的阶乘的长度
1058 N的阶乘的长度 基准时间限制:1 秒 空间限制:131072 KB 输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3. Input 输入N(1 <= N <= ...
- magento 切换数据库,使用不同数据库
1. 在app/etc/local.xml 中,添加新的数据库选项 <?xml version="1.0"?> <config> <global> ...
- GitLab使用方法
注意只有master权限的用户才可以push到主线master分支上(默认受保护)(当一个新版本的app定版之后,才会提交到master分支上,平时不建议使用该分支),developer没有push到 ...