[网络流]
先抄个模板
EK:
1 const int MAXN = 430; 2 const int MAX_INT = (1 << 30); 3 4 struct Edge{ 5 int v, nxt, w; 6 }; 7 8 struct Node{ 9 int v, id; 10 }; 11 12 int n, m, ecnt; 13 bool vis[MAXN]; 14 int head[MAXN]; 15 Node pre[MAXN]; 16 Edge edge[MAXN]; 17 18 void init(){ 19 ecnt = 0; 20 memset(edge, 0, sizeof(edge)); 21 memset(head, -1, sizeof(head)); 22 } 23 24 void addEdge(int u, int v, int w){ 25 edge[ecnt].v = v; 26 edge[ecnt].w = w; 27 edge[ecnt].nxt = head[u]; 28 head[u] = ecnt++; 29 } 30 31 bool bfs(int s, int t){ 32 queue <int> que; 33 memset(vis, 0, sizeof(vis)); 34 memset(pre, -1, sizeof(pre)); 35 pre[s].v = s; 36 vis[s] = true; 37 que.push(s); 38 while(!que.empty()){ 39 int u = que.front(); 40 que.pop(); 41 for(int i = head[u]; i + 1; i = edge[i].nxt){ 42 int v = edge[i].v; 43 if(!vis[v] && edge[i].w){ 44 pre[v].v = u; 45 pre[v].id = i; 46 vis[v] = true; 47 if(v == t) return true; 48 que.push(v); 49 } 50 } 51 } 52 return false; 53 } 54 55 int EK(int s, int t){ 56 int ans = 0; 57 while(bfs(s, t)){ 58 int mi = MAX_INT; 59 for(int i = t; i != s; i = pre[i].v){ 60 mi = min(mi, edge[pre[i].id].w); 61 } 62 for(int i = t; i != s; i = pre[i].v){ 63 edge[pre[i].id].w -= mi; 64 edge[pre[i].id ^ 1].w += mi; 65 } 66 ans += mi; 67 } 68 return ans; 69 } 70 71 // 加边 72 addEdge(u, v, w); 73 addEdge(v, u, 0); 74 // 调用 75 int ans = EK(s, t);
No matter how you feel, get up , dress up , show up ,and never give up.