POJ3020【二分匹配】
思路:
---说给自己
一开始想的是从1-h*w标记整幅图,建图是星号和 {他,与他相连的星号} 建边,肯定要去匹配"*"啊,所以空格一定不会去造,然后就理解成了最小点覆盖,然而对于最小点覆盖,对于 孤立点(也就是没有连出去的边) ,这样就错了。。。但是对于这个思路还是打完了,然后发现错了。。。其实对于“正确”思路要先想想是嘛。。。
---正解
这题题意是一个圈最多只能圈两个" * ",那么完全可以理解一个圈就是一条线,连了两个星号,那么直接星号和星号建边,记得建双向。求一个最小路径覆盖,这里因为是双向路径多了一倍,answer=num-path_num/2;
贴一发挫代码;
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <math.h>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f
#define pi acos(-1.0)
#define LL long long int ma[1010][1010];
int cx[1010],cy[1010];
int vis[1010];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int id_num[45][15];
char s[45][15];
int n,m;
int num; void init()
{
memset(ma,0,sizeof(ma));
num=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(s[i][j]=='o')
continue;
id_num[i][j]=++num;
}
} void pre_solve()
{
int u,v;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(s[i][j]=='o')
continue;
u=id_num[i][j];
for(int k=0;k<4;k++)
{
int x=dx[k]+i;
int y=dy[k]+j;
if(x<0||y<0||x>=n||y>=m||s[x][y]=='o')
continue;
v=id_num[x][y];
ma[u][v]=1;
}
}
} int find_path(int u)
{
for(int i=1; i<=num; i++)
{
if(!vis[i]&&ma[u][i])
{
vis[i]=1;
if(cy[i]==-1||find_path(cy[i]))
{
cy[i]=u;
return 1;
}
}
}
return 0;
} int solve()
{
int ans=0;
memset(cy,-1,sizeof(cy));
for(int i=1; i<=num; i++)
{
memset(vis,0,sizeof(vis));
if(find_path(i))
ans++;
}
return ans;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
init();
pre_solve();
printf("%d\n",num-solve()/2);
}
return 0;
}
POJ3020【二分匹配】的更多相关文章
- POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
- [kuangbin带你飞]专题十 匹配问题 二分匹配部分
刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...
- BZOJ 1189 二分匹配 || 最大流
1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1155 Solved: 420[Submi ...
- Kingdom of Obsession---hdu5943(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5943 题意:给你两个数n, s 然后让你判断是否存在(s+1, s+2, s+3, ... , s+n ...
- poj 2060 Taxi Cab Scheme (二分匹配)
Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5710 Accepted: 2393 D ...
- [ACM_图论] Sorting Slides(挑选幻灯片,二分匹配,中等)
Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...
- [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)
描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
- nyoj 237 游戏高手的烦恼 二分匹配--最小点覆盖
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=237 二分匹配--最小点覆盖模板题 Tips:用邻接矩阵超时,用数组模拟邻接表WA,暂时只 ...
- UVA5874 Social Holidaying 二分匹配
二分匹配简单题,看懂题意,建图比较重要. #include<stdio.h> #include<string.h> #define maxn 1100 int map[maxn ...
- HDU 2236:无题II(二分搜索+二分匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2236 题意:中文题意. 思路:先找出最大和最小值,然后二分差值,对于每一个差值从下界开始枚举判断能不能二分匹配. ...
随机推荐
- iOS8的UIPresentationController
本文转载至 http://kyfxbl.iteye.com/blog/2147888 从iOS8开始,controller之间的跳转特效,需要用新的API UIPresentationControll ...
- css常用总结
1.固定一个层在页面的位置,不受滚动条影响, 属性position:fixed,如: .tbar{ height:200px;width:60px;background-color:#666;posi ...
- Spring整合Hibernate的方法
一.基本支持 Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. Spring 对这些 ORM 框架的支持是一致的, 因此 ...
- Use Apache HBase™ when you need random, realtime read/write access to your Big Data.
Apache HBase™ is the Hadoop database, a distributed, scalable, big data store. Use Apache HBase™ whe ...
- Android笔记之平移View
方法有多种,只讲一种 使用View.setLeft和View.setRight 对于wrap_content的View,要横向平移,setRight是必要的,否则View的宽度会被改变(right应设 ...
- java中两字符串比较--compareTo方法
java.lang.String.compareTo() 方法比较两个字符串的字典,比较是基于字符串中的每个字符的Unicode值 String n1 = "1"; String ...
- html video api控件总结
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使用bat文件打开和关闭本地exe
打开: cd 路径start AA.exe 关闭: taskkill /f /im AA.exe
- Vue源码探究-状态初始化
Vue源码探究-状态初始化 Vue源码探究-源码文件组织 Vue源码探究-虚拟DOM的渲染 本篇代码位于vue/src/core/instance/state.js 继续随着核心类的初始化展开探索其他 ...
- zoom:1
zoom这个特性是IE特有的属性. zoom:1;一般是拿来解决IE6的子元素浮动时候父元素不随着自动扩大的问题,功能相当于overflow:auto,同样也可以用height:1%来代替zoom ...