1. 最大流
const int N = 200; const int M = N*N; int head[N], deep[N], cur[N]; int w[M], to[M], nx[M]; int tot; void add(int u, int v, int val){ w[tot] = val; to[tot] = v; nx[tot] = head[u]; head[u] = tot++; w[tot] = 0; to[tot] = u; nx[tot] = head[v]; head[v] = tot++; } int bfs(int s, int t){ queue<int> q; memset(deep, 0, sizeof(deep)); q.push(s); deep[s] = 1; while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; ~i; i = nx[i]){ if(w[i] > 0 && deep[to[i]] == 0){ deep[to[i]] = deep[u] + 1; q.push(to[i]); } } } return deep[t] > 0; } int Dfs(int u, int t, int flow){ if(u == t) return flow; for(int &i = cur[u]; ~i; i = nx[i]){ if(deep[u]+1 == deep[to[i]] && w[i] > 0){ int di = Dfs(to[i], t, min(w[i], flow)); if(di > 0){ w[i] -= di, w[i^1] += di; return di; } } } return 0; } int Dinic(int s, int t){ int ans = 0, tmp; while(bfs(s, t)){ for(int i = 0; i <= t; i++) cur[i] = head[i]; while(tmp = Dfs(s, t, inf)) ans += tmp; } return ans; } void init(){ memset(head, -1, sizeof(head)); tot = 0; }