POJ3041--Asteroids--二分图最大匹配--Konig
2013-02-17 01:12
316 查看
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.
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.
Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.
Sample Input
Sample Output
Hint
INPUT DETAILS:
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
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.
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.
Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.
Sample Input
3 4 1 1 1 3 2 2 3 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.
//此题用到Konig定理。即最少覆盖数==最大匹配数。
然后就是个很简单的二分图最大匹配问题。
X集为横坐标,Y集为纵坐标。xiyi边为炸弹。则只需要每条边(即每个炸弹)至少有一个点和xi或者yi关联。然后就是Konig
#include <iostream> #include <cstdio> #include <vector> using namespace std; vector <int> ans[1008]; bool vis[1008]; int marry[1008]; int sum; bool dfs(int u) { for(int i=0;i<ans[u].size();i++) { if(!vis[ans[u][i]]) { vis[ans[u][i]]=1; if(!marry[ans[u][i]]||dfs(marry[ans[u][i]])) { marry[ans[u][i]]=u;//这里不用互为夫妻。。因为dfs的永远是一方。 return 1; } } } return 0; } int main() { int n,k;//n<=500,k<=10000 while(scanf("%d%d",&n,&k)==2) { sum=0; int u,v; for(int i=1;i<=2*n+10;i++) { ans[i].clear(); } for(int i=1;i<=k;i++) { scanf("%d%d",&u,&v); v=v+n; ans[u].push_back(v); } for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(dfs(i)) { sum++; } } cout<<sum<<endl; } return 0; }
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 1008 #define edge 21000 #define inf 0x3f3f3f3f int first[maxn],dis[maxn],num[maxn]; int vv[edge],ww[edge],nxt[edge]; int e,NN; void addEdge(int u,int v,int w) { vv[e] = v;ww[e] = w;nxt[e] = first[u];first[u] = e++; vv[e] = u;ww[e] = 0;nxt[e] = first[v];first[v] = e++; } inline int min(int a,int b) { return a>b?b:a; } int dfs(int u,int s,int d,int cost) { if(u == d) return cost; int ans = 0; int _min = NN; for(int i=first[u];i!=-1;i=nxt[i]) { int v = vv[i]; if(ww[i]) { if(dis[v] + 1 == dis[u]) { int t = dfs(v,s,d,min(ww[i],cost)); ww[i] -= t; ww[i^1] += t; ans += t; cost -= t; if(dis[s] == NN) return ans; if(!cost) break; } if(_min > dis[v]) _min = dis[v]; } } if(!ans) { if(--num[dis[u]] == 0) dis[s] = NN; dis[u] = _min + 1; ++num[dis[u]]; } return ans; } int isap(int s,int d) { memset(dis,0,sizeof(dis)); memset(num,0,sizeof(num)); num[0] = NN; int ans = 0; while(dis[s] < NN) ans += dfs(s,s,d,inf); return ans; } int main() { //freopen("in.txt","r",stdin); int n,m; while(scanf("%d%d",&n,&m)==2) { memset(first,-1,sizeof(first)); e = 0; NN = 2*n + 2; for(int i=1;i<=n;i++) { addEdge(0,i,1); } for(int i=n+1;i<=2*n;i++) { addEdge(i,2*n+1,1); } for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); v += n; addEdge(u,v,1); } int ans = isap(0,2*n+1); printf("%d\n",ans); } return 0; }
相关文章推荐
- poj3041-Asteroids , 二分图的最小顶点覆盖数 = 最大匹配数
- poj3041-Asteroids , 二分图的最小顶点覆盖数 = 最大匹配数
- poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配
- NYOJ 237 游戏高手的烦恼 && POJ3041-Asteroids ( 二分图的最大匹配 )
- (二分图最大匹配) poj3041 Asteroids
- POJ3041——Asteroids(二分图最大匹配)
- 二分图的最大匹配 ————匈牙利算法 (转载了一个大神的趣味算法) poj3041(Asteroids)
- POJ3041 Asteroids(二分图最大匹配)
- [POJ3041]Asteroids(二分图,最大匹配)
- POJ3041 Asteroids 二分图最小顶点覆盖 Dinic求解最大流
- POJ训练计划3041_Asteroids(二分图/最小点覆盖=最大匹配)
- poj 3041 Asteroids (二分图最大匹配 == 最小点覆盖数)
- Asteroids_poj3041_最大匹配
- poj3041-Asteroids(最小点覆盖,最大匹配)
- CUGB图论专场2:B - Asteroids 二分图:最小顶点覆盖=最大匹配数
- POJ3041--二分图最大匹配模板
- poj 3041 Asteroids 二分图的最大匹配==最小覆盖点。
- poj-3041-Asteroids -二分图的最大匹配
- poj3041 二分图 最大匹配数
- poj 3041 Asteroids 初级-》二分图最大匹配