【二分图|最大匹配】POJ-3041 Asteroids(匈牙利算法dfs、bfs版)
来源:互联网 发布:全军战备值班部队 知乎 编辑:程序博客网 时间:2024/06/08 19:43
Asteroids
Time Limit: 1000MS Memory Limit: 65536K
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
————————————————————寝る前の分割線————————————————————
思路:水题不解释,注意行列编号从1开始。
dfs版代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <climits>#include <iostream>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 555, M = 11111;int tot, head[N], cou[N], n, m;bool vis[N];struct Edge {int v, next;Edge(){}Edge(int _v, int _next):v(_v), next(_next){}}edge[M];void init(){tot = 0;memset(head, -1, sizeof(head));}void add(int u, int v){edge[tot] = Edge(v, head[u]);head[u] = tot++;}int dfs(int u){for(int i = head[u]; ~i; i = edge[i].next) {int v = edge[i].v;if(!vis[v]) {vis[v] = 1;if(cou[v] == -1 || dfs(cou[v])) {cou[v] = u;return 1;}}}return 0;}int match(){int ret = 0;memset(cou, -1, sizeof(cou));for(int i = 1; i <= n; i++) {memset(vis, 0, sizeof(vis));ret += dfs(i);}return ret;}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endifwhile(~scanf("%d%d", &n, &m)) {int u, v;init();for(int i = 0; i < m; i++) {scanf("%d%d", &u, &v);add(u, v);}int ans = match();printf("%d\n", ans);}return 0;}
bfs版代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <climits>#include <iostream>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 555, M = 11111;int tot, head[N], mx[N], my[N], pre[N], n, m;int vis[N];struct Edge { int v, next; Edge(){} Edge(int _v, int _next): v(_v), next(_next){}}e[M];void init(){ tot = 0; memset(head, -1, sizeof(head));}void add(int u, int v){ e[tot] = Edge(v, head[u]); head[u] = tot++;}bool bfs(int st){ queue <int> q; q.push(st); pre[st] = -1; bool flag = false; while(!q.empty() && !flag) { int u = q.front(); q.pop(); for(int i = head[u]; ~i && !flag; i = e[i].next) { int v = e[i].v; if(vis[v] != st) { vis[v] = st; q.push(my[v]); if(~my[v]) pre[my[v]] = u; else {//找到增广路 int a = u, b = v; flag = true; while(~a) { int t = mx[a]; mx[a] = b; my[b] = a; a = pre[a]; b = t; } } } } } return mx[st] != -1;}int hungary(){ int ret = 0; memset(mx, -1, sizeof(mx)); memset(my, -1, sizeof(my)); memset(vis, -1, sizeof(vis)); for(int i = 1; i <= n; i++) {//number from 1 if(mx[i] == -1) { if(bfs(i)) ret++; } } return ret;}int main(){#ifdef J_Sure// freopen("000.in", "r", stdin);// freopen(".out", "w", stdout);#endif while(~scanf("%d%d", &n, &m)) { int u, v; init(); for(int i = 0; i < m; i++) { scanf("%d%d", &u, &v); add(u, v); } int ans = hungary(); printf("%d\n", ans); } return 0;}
0 0
- 【二分图|最大匹配】POJ-3041 Asteroids(匈牙利算法dfs、bfs版)
- poj 3041 Asteroids (匈牙利算法---二分图最大匹配)
- POJ 3041 Asteroids 匈牙利算法 二分图最大匹配
- POJ 3041 Asteroids(二分图匹配+匈牙利算法)
- 【二分图匹配】最大匹配-匈牙利算法BFS && DFS写法
- (模板题)poj 3041 Asteroids(二分图的最大匹配匈牙利算法)
- POJ 3041 Asteroids 二分图匹配 匈牙利算法
- poj 3041 Asteroids(二分图匹配匈牙利算法)
- poj 3041 Asteroids(二分图匹配匈牙利算法)
- poj 3041 Asteroids【二分图】【匈牙利算法】
- poj Asteroids (二分匹配之匈牙利算法)
- poj 3041 Asteroids 二分匹配 匈牙利算法 模板题
- POJ 3014:Asteroids(二分匹配,匈牙利算法)
- 二分图最大匹配(匈牙利算法-DFS增广模板)
- Asteroids(poj 3041,二分图最大匹配)
- [二分图最大匹配] poj 3041 Asteroids
- POJ 3041 Asteroids (二分图最大匹配)
- poj 3041 Asteroids 二分图最大匹配
- JAVA程序设计(15.1)----- 图形用户界面GUI 制作红绿灯~
- init()和onEnter()方法的区别
- JVM学习笔记
- WinXP下打造自己的linux 0.11简易编译环境(原创)
- remove element
- 【二分图|最大匹配】POJ-3041 Asteroids(匈牙利算法dfs、bfs版)
- iOS 在TabViewController中设置Tab Bar,且实现自定义
- LeetCode LRU Cache (Java实现)
- Git代码分支管理在项目中的实践
- 将一组按以下规则排序:第一个数最大,第二个数最小,第三个数是剩下中最大的,第四个数是剩下中最小的,依次类推。
- python面向对象编程
- Install Composer on CentOS
- C语言日期倒计时
- JSP(三)