poj3041 Asteroids //二分图匹配(Dinic实现)
Asteroids
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27596 Accepted: 14814 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
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.
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).Source
题意:要求出最小覆盖点(最少的点连接到所有的边),因为 对于二分图而言 最小覆盖点==最大匹配
所以建立最大匹配模型 当前的点是某一行或者某一列。
建立左边是行号,右边是列号+n,将有行星的行列连接对于样例如图:
最少的点连接到所有的边,即对点进行二分图匹配。
这里使用最大流算法,(因为不想学二分图匹配算法) 复杂度不稳定(较大的数据也可以过),最大是O(N^2*E)
二分匹配求的就是一个最大的匹配数量,所以可以等效为边容量为1点最大流。
Time : 32ms
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<climits>
#include<cmath>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
#define LL long long
#define mod 1000000007
const int maxn=1e3+5;
int n,k;
struct no{int to,cap,rev;}; //arc
vector<no>g[maxn]; //图
int level[maxn]; //到起点的距离
int iter[maxn]; //当前弧,在其之前的边已经没用了
void addarc(int s,int e,int c){
g[s].push_back((no){e,c,g[e].size()});
g[e].push_back((no){s,0,g[s].size()-1});
}
//更新层次,即level
void bfs(int s){
memset(level,-1,sizeof(level));
level[s]=0;
queue<int>q;
q.push(s);
while(!q.empty()){
int now=q.front();q.pop();
for(int i=0;i<(int)g[now].size();i++){
no &arc=g[now][i];
if(level[arc.to]!=-1||arc.cap<=0) continue;
level[arc.to]=level[now]+1;
q.push(arc.to);
}
}
}
//寻找增广路
int dfs(int v,int t,int f){
if(v==t) return f;
for(iter[v];iter[v]<(int)g[v].size();iter[v]++){
no &arc=g[v][iter[v]];
if(arc.cap<=0||level[arc.to]!=level[v]+1) continue;
int d=dfs(arc.to,t,min(f,arc.cap));
if(d>0) {
arc.cap=arc.cap-d;
g[arc.to][arc.rev].cap+=d;
return d;
}
}
return 0;
}
int Dinic(int s,int t){
int re=0;
while(1){
bfs(s);
memset(iter,0,sizeof(iter));
if(level[t]==-1) return re;
int f;
while((f=dfs(s,t,INT_MAX))>0)
re=re+f;
}
return re;
}
int bipartite_matching(){
return Dinic(0,2*n+1);
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k;
for(int i=1;i<=k;i++) {
int r,c;cin>>r>>c;
addarc(r,c+n,1);
}
for(int i=1;i<=n;i++) addarc(0,i,1);
for(int i=n+1;i<=2*n;i++) addarc(i,2*n+1,1);
cout<<bipartite_matching();
return 0;
}