标签:poj3041 asteroids 最大流 最小点覆盖 二分图匹配
Description
Input
Output
Sample Input
3 4 1 1 1 3 2 2 3 2
Sample Output
2
Hint
#include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<cstdio> using namespace std; const int MAXN = 1210; const int MAXM = 21000; const int INF = 0x3f3f3f3f; struct Edge { int from, to, cap, next; }; Edge edge[MAXM]; int level[MAXN]; int head[MAXN]; int src, des, cnt; void addedge(int from,int to,int cap) { edge[cnt].from = from; edge[cnt].to = to; edge[cnt].cap = cap; edge[cnt].next = head[from]; head[from] = cnt++; swap( from, to ); edge[cnt].from = from; edge[cnt].to = to; edge[cnt].cap = 0; edge[cnt].next = head[from]; head[from] = cnt++; } int bfs() { memset( level, -1, sizeof level ); queue<int> q; while(!q.empty()) q.pop(); level[src] = 0; q.push( src ); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(edge[i].cap&&level[v] == -1) { level[v] = level[u] + 1; q.push( v ); } } } return level[des] != -1; } int dfs( int u, int f ) { if(u == des) return f; int tem; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(edge[i].cap > 0 && level[v] == level[u] + 1) { tem = dfs( v, min( f, edge[i].cap ) ); if(tem > 0) { edge[i].cap -= tem; edge[i^1].cap += tem; return tem; } } } level[u] = -1; return 0; } int Dinic() { int ans = 0, tem; while(bfs()) { while((tem = dfs( src, INF ) > 0)) { ans += tem; } } return ans; } int main() { int n, k; src = 0; des = 1205; while(cin >> n >> k) { memset( head, -1, sizeof head ); cnt = 0; for(int i = 1; i <= n; i++) { addedge( src, i, 1 ); addedge( i + 500, des, 1 ); } for(int i = 1; i <= k; i++) { int a, b; cin >> a >> b; addedge( a, b + 500, 1 ); } cout << Dinic() << endl; } return 0; }
标签:poj3041 asteroids 最大流 最小点覆盖 二分图匹配
原文地址:http://blog.csdn.net/maxichu/article/details/45332121