poj3041
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12162 | Accepted: 6620 |
Description
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
Output
Sample Input
3 4
1 1
1 3
2 2
3 2
Sample Output
2
Hint
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
#include<iostream>
using namespace std; int n,k; //n矩阵规格,k星体数量
int V1,V2; //二分图顶点集
/*矩阵的行列分别属于二分图的两个顶点集V1、V2,其中行x∈V1,列y∈V2*/
bool grid[501][501]; //存储数据方式:可达矩阵
bool vis[501]; //记录V2的点每个点是否已被搜索过
int link[501]; //记录 V2中的点y 在 V1中 所匹配的点x的编号
int m; //最大匹配数 /*Hungary Algorithm*/ bool dfs(int x)
{
for(int y=1;y<=V2;y++)
if(grid[x][y] && !vis[y]) //x到y相邻(有边) 且 节点y未被搜索
{
vis[y]=true; //标记节点y已被搜索
if(link[y]==0 || dfs(link[y])) //link[y]==0 : 如果y不属于前一个匹配M
{ //find(link[y] : 如果被y匹配到的节点可以寻找到增广路
link[y]=x; //那么可以更新匹配M'(用M替代M')
return true; //返回匹配成功的标志
}
}
return false; //继续查找V1下一个x的邻接节点
} void search(void)
{
for(int x=1;x<=V1;x++)
{
memset(vis,false,sizeof(vis)); //清空上次搜索时的标记
if(dfs(x)) //从V1中的节点x开始寻找增广路
m++;
}
return;
} int main(void)
{
cin>>n>>k;
V1=V2=n; int x,y; //坐标(临时变量)
for(int i=1;i<=k;i++)
{
cin>>x>>y;
grid[x][y]=true; //相邻节点标记
} /*增广轨搜索*/ search(); /*Output*/ cout<<m<<endl; return 0;
}
poj3041的更多相关文章
- poj3041,poj2226
二分匹配的灵活运用 3041还是比较好想的,考虑到横排/竖排射一枪就能搞定这一行/一列的所有点, 我们以行数为点集x,列数为点集y,在目标点(xi,yi)之间连一条边 这样最小射击次数=最小点覆盖(边 ...
- POJ3041 二分图最大匹配
问题:POJ3041 分析: 构造二分图:令A = B = { 1, 2, ... , n }, 分别代表行号集与列号集.假如第i行第j列有一颗行星,则连接Ai与Bj, 表示必须从Ai(即第i行),B ...
- POJ3041 Asteroids 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8229200.html 题目传送门 - POJ3041 题意概括 有一个n*n的矩阵,有些点是障碍物. 现在每次可 ...
- poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配
/** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...
- POJ-3041 Asteroids---二分图&最小覆盖点
题目链接: https://vjudge.net/problem/POJ-3041 题目大意: 给一个N*N的矩阵,有些格子有障碍,要求我们消除这些障碍,问每次消除一行或一列的障碍, 最少要几次. 解 ...
- 【题解】POJ3041 Asteroids - 图论 - 二分图匹配
声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 POJ3041 Asteroids 题目描述 假如你现在正处在一个 \(N*N\ ...
- POJ-3041
思路:将n个行看作n个点{x_i}(i=1, ..., n),n个列也看作n个点{y_j}(j=1, ..., n).每个障碍看作一条无向边(x_i, y_j).则该问题能够归结为求二分图最小点覆盖数 ...
- poj3041 二分图最小顶点覆盖
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17237 td>Accepted: 9375 ...
- POJ3041 Asteroids(二分图最大匹配)
题目链接. 分析: 暂略. AC代码: #include <iostream> #include <cstdio> #include <cstring> #incl ...
随机推荐
- 类handler
/** The handler class is the interface for dynamically loadable storage engines. Do not add ifdefs a ...
- if(username.equals(“zxx”){}
1. if(username.equals(“zxx”){} username可能为NULL,会报空指针错误:改为"zxx".equals(username) 2. int x ...
- CodeForces 489B (贪心 或 最大匹配) BerSU Ball
题意: 有m个男孩和n个女孩,每个人都有一个舞蹈熟练度,用一个不超过100的正整数来表示. 一个男孩和一个女孩能够结为舞伴当且仅当两人的熟练度相差不超过1. 问最多能结成多少对舞伴 分析: 这是一个二 ...
- UVa 10285 Longest Run on a Snowboard【记忆化搜索】
题意:和最长滑雪路径一样, #include<iostream> #include<cstdio> #include<cstring> #include <c ...
- SelectSingleNode和SelectNodes区别
SelectSingleNode:选择匹配 XPath 表达式的第一个 XmlNodeSelectNodes:选择匹配 XPath 表达式的结点集合 XmlNodeList
- 编写爬虫(spider)的预备知识:用java发送HTTP请求
使用原生API来发送http请求,而不是使用apache的库,原因在于这个第三方库变化实在太快了,每个版本都有不小的变化.对于程序员来说,使用它反而会有很多麻烦,比如自己曾经写过的代码将无法复用. 原 ...
- 【转】堆栈跟踪中收到一个UnhandledExceptionFilter调用时,如何查找问题异常堆栈
定义没有异常处理程序来处理引发的异常时调用UnhandledExceptionFilter函数.函数通常将异常传递到捕获并处理它所尝试的 Ntdll.dll 文件. 在某些情况下,在其中存在的进程内存 ...
- hdu 2459 (后缀数组+RMQ)
题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...
- HDU 5679 Substring 后缀数组判重
题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...
- IOS init initWith 等相关集中
1.initWithCoder 当一个view从nib初始化的时候,会调用这个函数. 用keyedArchiver序列化一个类的实力,后面用keyedUnArchiver拿回来的时候会调用到 ...