pku 3041 Asteroids
来源:互联网 发布:迪杰斯特拉算法 严蔚敏 编辑:程序博客网 时间:2024/05/23 00:09
Description
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid.
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.
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
* Line 1: Two integers N and K, separated by a single space.
* 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.
* 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
* Line 1: The integer representing the minimum number of times Bessie must shoot.
Sample Input
3 41 11 32 23 2
Sample Output
2
Hint
INPUT DETAILS:
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).
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).
KEY:
这题貌似好早学长们布置的题目……当时也有人讲了……貌似还是不会……今天看二分图匹配, 突然想去做,居然神奇的AC了。
这题用的是二分图匹配的匈牙利算法。
- Source Code
- #include<iostream>
- #define N 500
- using namespace std;
- int a[N+1][N+1];
- int n,m;
- int match[N+1];
- int check[N+1];
- int dfs(int p)
- {
- int i,t;
- for(i=1;i<=n;i++)
- if(a[i][p]&&!check[i])
- {
- check[i]=1;
- t=match[i];
- match[i]=p;
- if(t==0||dfs(t)) return 1;
- match[i]=t;
- }
- return 0;
- }
- void input()
- {
- scanf("%d %d",&n,&m);
- int i;
- int x,y;
- for(i=1;i<=m;i++)
- {
- scanf("%d %d",&x,&y);
- a[x][y]=1;
- }
- }
- int main()
- {
- // freopen("pku_3041.in","r",stdin);
- input();
- int i,max=0;
- for(i=1;i<=n;i++)
- {
- memset(check,0,sizeof(check));
- if(dfs(i)) max++;
- }
- printf("%d/n",max);
- return 0;
- }
- pku 3041 Asteroids
- pku 3041 Asteroids
- pku 3041 Asteroids(最小点覆盖,最大二分匹配)
- PKU 3041 Asteroids - 行列的二分图模型 最小点覆盖
- Pku acm 3041 Asteroids 数据结构题目解题报告(十六)---- 匈牙利算法求二分图的最大匹配
- POJ 3041 Asteroids / UESTC 253 Asteroids
- 3041 Asteroids //MaxMatch
- POJ 3041 Asteroids
- poj 3041 Asteroids
- POJ-3041 Asteroids
- Poj 3041 Asteroids
- poj 3041 Asteroids
- poj 3041 Asteroids
- POJ:3041Asteroids
- poj 3041 Asteroids
- POJ 3041 Asteroids
- POJ 3041 Asteroids
- POJ 3041 Asteroids
- Subversion权限详解
- VC6 使用技巧大全
- VS2003的调试症状:“试图运行项目时出错:无法在web服务器上启动调试。”的解决方法
- linux用户管理命令(添加,删除,修改)
- The Life Cycle of a JSP Page
- pku 3041 Asteroids
- Struts2中使用request,response对象方法
- Web实用小工具推荐使用
- 更新XML字段时遇到的中文问题
- 基于MC39I的电能管理终端远程GPRS接口设计
- 关于encode_one_macroblock()中的一个for循环
- petshop学习笔记(1)
- C#中的随机数种子
- 如何知道某个进程名所对应的进程号