二分多重匹配(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 ...
随机推荐
- 迷宫bfs POJ3984
#include<stdio.h> int map[5][5]={0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, ...
- Python学习笔记——1——基础知识
1.1.变量和算法 python语言很类似人类语言,变量不需要定义类型.比如: 整型 字符串类型 数组 Java int a=12 String s="test" String[] ...
- session和cookie区别
<?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- rpc rmi http
1.RPC与RMI (1)RPC 跨语言,而 RMI只支持Java. (2)RMI 调用远程对象方法,允许方法返回 Java 对象以及基本数据类型,而RPC 不支持对象的概念,传送到 RPC 服务的消 ...
- 软件工程概论---max单元测试
题目:一个单元测试,查找list[]中的最大值 编写一个程序对Largest函数进行测试,列举所有测试用例. 思路:首先确保数组不为空,和数组长度不为0,否则输入错误.根据老师所给的函数写一个主函数, ...
- Javascript实现提示错误的信息直接出现在输入框后
可以在输入框后加个<span id="error"></span>,当验证错误时这样处理document.getElementById('error').i ...
- E1114 Temp Ambient
这2天DELL服务器的指示灯变为了黄色 ,显示“ E1114 Ambient Temp exceeds allowed range“ 原来是周围环境温度超出了许可范围 ,难道最近的天真的是太冷了 ”
- Maven集成Sonar
Sonar对maven提供了简单可配的支持,要做的事情很简单--在maven/conf下settings.xml <profiles></profiles>标签之间添加如下内容 ...
- 人脸识别的ppt
http://wenku.baidu.com/link?url=GWiNRy_9mzqBZ9_2DDZbHRZbBbaBofhs1Y0pBS7aiE5oab8TY78r_z6aAlqo_L9AZzD4 ...
- 读取XML文档结构并写入内容
1.在项目中新建XML文档结构.xsd文件,在其中添加相应的节点. 2.读取文档结构并写入内容 string initFileName = @"D:\Config.xml"; Da ...