POJ 3041 Asteroids 匈牙利算法,最大流解法,行列为点 难度:1
http://poj.org/problem?id=3041
#include <cstdio> #include <cstring> #include <vector> using namespace std; int e[1004][1004]; bool vis[1004]; vector <int > G[1004]; int n,m; int dfs(int s){ vis[s]=true; if(s==n*2+1){//printf("dfs ok %d\n",s); return 1;} for(int i=0;i<G[s].size();i++){ int t=G[s][i]; if(!vis[t]&&e[s][t]>0){ if(dfs(t)>0){ e[s][t]-=1; e[t][s]+=1; //printf("dfs ok %d\n",s); return 1; } } } // printf("dfs failed %d\n",s); return 0; } void printe(){ for(int i=0;i<=2*n+1;i++){ for(int j=0;j<=2*n+1;j++){ if(e[i][j])printf("e[%d][%d]%d ",i,j,e[i][j]); } printf("\n"); } printf("\n"); } int maxflow(){ //printe(); int ans=0,f; while(1){ f=dfs(0); //printe(); memset(vis,0,sizeof(vis)); if(f==0)return ans; ans+=f; } } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){e[0][i]=1;e[i][0]=0;G[0].push_back(i);} for(int i=n+1;i<=2*n;i++){e[i][2*n+1]=1;e[2*n+1][i]=0;G[i].push_back(2*n+1);} for(int i=0;i<m;i++){ int f,t; scanf("%d%d",&f,&t); e[f][n+t]=1; e[n+t][f]=0; G[f].push_back(n+t); G[n+t].push_back(f); } int ans=maxflow(); printf("%d\n",ans); return 0; }