dinic网络流模板
src:源点
sink:汇点
#include<queue> #include<iostream> #include<string.h> #include<stdio.h> using namespace std; const int inf = 1000000000; const int maxn = 20000, maxm = 500000; struct Edge{ int v, f, nxt; }; int src, sink; int g[maxn + 10]; int nume; Edge e[maxm*2+10]; void addedge(int u, int v, int c){ e[++nume].v = v; e[nume].f = c; e[nume].nxt = g[u]; g[u] = nume; e[++nume].v = u; e[nume].f = 0; e[nume].nxt = g[v]; g[v] = nume; } void init(){ memset(g, 0, sizeof(g)); nume = 1; } queue<int> que; bool vis[maxn +10]; int dist[maxn + 10]; void bfs(){ memset(dist, 0, sizeof(dist)); while(!que.empty()) que.pop(); vis[src] = 1; que.push(src); while(!que.empty()){ int u = que.front(); que.pop(); for(int i = g[u]; i; i = e[i].nxt) if(e[i].f && !vis[e[i].v]){ que.push(e[i].v); dist[e[i].v] = dist[u] + 1; vis[e[i].v] = true; } } } int dfs(int u, int delta){ if(u == sink){ return delta; }else{ int ret = 0; for(int i = g[u]; delta && i; i = e[i].nxt){ if(e[i].f && dist[e[i].v] == dist[u] +1){ int dd = dfs(e[i].v, min(e[i].f, delta)); e[i].f -= dd; e[i^1].f += dd; delta -= dd; ret += dd; } } return ret; } } int maxflow(){ int ret = 0; while(true){ memset(vis, 0, sizeof(vis)); bfs(); if(!vis[sink])return ret; ret += dfs(src, inf); } return ret; } int main(){ int n, m; while(scanf("%d%d", &n, &m)!=EOF){ init(); src = 1; sink = m; for(int i = 0; i < n; i++){ int x, y, z; scanf("%d%d%d", &x, &y, &z); addedge(x, y, z); } printf("%d\n", maxflow()); } }