刷刷基础题来巩固一下基础..
#1.飞行员配对方案问题
听说各大OJ的题面都和pdf不同..
嗯连边匹配就行..
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> using namespace std; const int Maxn = 110; struct node { int y, next, c, opp; }a[Maxn*Maxn*4]; int first[Maxn*2], len; void ins(int x, int y, int c) { len++; int k1 = len; a[len].y = y; a[len].c = c; a[len].next = first[x]; first[x] = len; len++; int k2 = len; a[len].y = x; a[len].c = 0; a[len].next = first[y]; first[y] = len; a[k1].opp = k2; a[k2].opp = k1; } int st, ed, h[Maxn*2]; int n, m; bool bfs() { queue <int> q; memset(h, -1, sizeof(h)); h[st] = 0; q.push(st); while(!q.empty()){ int x = q.front(); q.pop(); for(int k = first[x]; k; k = a[k].next){ int y = a[k].y; if(h[y] == -1 && a[k].c > 0){ h[y] = h[x]+1; q.push(y); } } } return h[ed] > 0; } int _min(int x, int y) { return x < y ? x : y; } int dfs(int x, int flow) { if(x == ed) return flow; int delta = 0; for(int k = first[x]; k; k = a[k].next){ int y = a[k].y; if(h[y] == h[x]+1 && a[k].c > 0 && flow-delta > 0){ int minf = dfs(y, _min(a[k].c, flow-delta)); delta += minf; a[k].c -= minf; a[a[k].opp].c += minf; } } if(delta == 0) h[x] = -1; return delta; } int main() { int i, j, k; scanf("%d%d", &n, &m); st = 0; ed = n+1; int x, y; while(scanf("%d%d", &x, &y) != EOF){ ins(x, y, 1); } for(i = 1; i <= m; i++) ins(st, i, 1); for(i = m+1; i <= n; i++) ins(i, ed, 1); int ans = 0; while(bfs()) ans += dfs(st, 0x7fffffff); printf("%d\n", ans); return 0; }
网络流24题
时间: 2024-10-12 21:16:52