1 //EK算法 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 8 const int N = 210,INF = (int)1e9; 9 int n,m,tot; 10 int head[N],pre[N],res[N]; 11 queue<int > que; 12 struct node{ 13 int to,w,nxt; 14 }e[N << 1]; 15 16 inline void init(){ 17 while(!que.empty()) que.pop(); 18 for(int i = 1; i <= n; ++i) res[i] = 0; 19 } 20 21 inline void add(int u,int v,int w){ 22 e[tot].to = v; e[tot].w = w; e[tot].nxt = head[u]; head[u] = tot++; 23 } 24 25 bool bfs(int s,int t){ 26 init(); 27 pre[s] = -1; 28 res[s] = INF; 29 que.push(s); 30 int now,to; 31 while(!que.empty()){ 32 now = que.front(); que.pop(); 33 for(int o = head[now]; ~o; o = e[o].nxt){ 34 to = e[o].to; 35 if(!res[to] && e[o].w){ 36 pre[to] = o; 37 res[to] = min(e[o].w, res[now]); 38 if(to == t) return true; 39 que.push(to); 40 } 41 } 42 } 43 return false; 44 } 45 46 int EK(int s,int t){ 47 int ans = 0; 48 while(bfs(s,t)){ 49 for(int o = pre[t]; ~o; o = pre[e[o^1].to]){ 50 e[o].w -= res[t]; 51 e[o^1].w += res[t]; 52 } 53 ans += res[t]; 54 } 55 return ans; 56 } 57 58 int main(){ 59 60 int u,v,w; 61 while(~scanf("%d%d",&m,&n)){ 62 for(int i = 1; i <= n; ++i) head[i] = -1; tot = 0; 63 for(int i = 1; i <= m; ++i){ 64 scanf("%d%d%d",&u,&v,&w); 65 add(u,v,w); add(v,u,0); 66 } 67 printf("%d ",EK(1,n)); 68 } 69 70 return 0; 71 }
1 //Dinic算法 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 8 const int N = 10010,M = 100010,inf = (int)1e9; 9 int n,m,s,t,tot; 10 int head[N],lev[N]; 11 queue<int > que; 12 struct node{ 13 int to,w,nxt; 14 }e[M<<1]; 15 16 inline void add(int u,int v,int w){ 17 e[tot].to = v; e[tot].w = w; e[tot].nxt = head[u]; head[u] = tot++; 18 } 19 20 inline void init(){ 21 while(!que.empty()) que.pop(); 22 for(int i = 1; i <= n; ++i) lev[i] = 0; 23 } 24 25 bool bfs(int s,int t){ 26 lev[s] = 1; 27 que.push(s); 28 int now,to; 29 while(!que.empty()){ 30 now = que.front(); que.pop(); 31 if(now == t) return true; 32 for(int o = head[now]; ~o; o = e[o].nxt){ 33 to = e[o].to; 34 if(!lev[to] && e[o].w){ 35 lev[to] = lev[now] + 1; 36 que.push(to); 37 } 38 } 39 } 40 return false; 41 } 42 43 int dfs(int now,int flow,int t){ 44 if(now == t) return flow; 45 int to,sum = 0,tmp; 46 for(int o = head[now]; ~o; o = e[o].nxt){ 47 to = e[o].to; 48 if(e[o].w && lev[now] == lev[to] - 1){ 49 tmp = dfs(to,min(flow - sum, e[o].w),t); 50 e[o].w -= tmp; e[o^1].w += tmp; 51 if((sum += tmp) == flow) return sum; 52 } 53 } 54 return sum; 55 } 56 57 int main(){ 58 59 int u,v,w; 60 scanf("%d%d%d%d",&n,&m,&s,&t); 61 for(int i = 1; i <= n; ++i) head[i] = -1; 62 for(int i = 1; i <= m; ++i){ 63 scanf("%d%d%d",&u,&v,&w); 64 add(u,v,w); add(v,u,0); 65 } 66 int ans = 0; 67 while(bfs(s,t)){ 68 ans += dfs(s,inf,t); 69 init(); 70 } 71 printf("%d ",ans); 72 73 return 0; 74 }
1 //最小费用最大流 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 8 const int N = (int)5e3+100,M = (int)5e4+100,INF = (int)1e9; 9 int n,m,s,t,tot; 10 int head[N],pre[N],dis[N],vis[N]; 11 queue<int > que; 12 struct node{ 13 int to,nxt,cap,cost,flow; 14 }e[M << 1]; 15 16 inline void add(int u,int v,int cap,int cost,int flow){ 17 e[tot].to = v; 18 e[tot].cap = cap; 19 e[tot].cost = cost; 20 e[tot].flow = flow; 21 e[tot].nxt = head[u]; 22 head[u] = tot++; 23 } 24 25 inline void init(){ 26 for(int i = 1; i <= n; ++i){ 27 vis[i] = false; 28 dis[i] = INF; 29 pre[i] = -1; 30 } 31 while(!que.empty()) que.pop(); 32 } 33 34 bool spfa(int s,int){ 35 init(); 36 vis[s] = true; dis[s] = 0; que.push(s); 37 int now,to; 38 while(!que.empty()){ 39 now = que.front(); que.pop(); 40 vis[now] = false; 41 for(int o = head[now]; ~o; o = e[o].nxt){ 42 to = e[o].to; 43 if(e[o].cap > e[o].flow && dis[to] > dis[now] + e[o].cost){ 44 dis[to] = dis[now] + e[o].cost; 45 pre[to] = o; 46 if(!vis[to]){ 47 vis[to] = true; que.push(to); 48 } 49 } 50 } 51 } 52 if(pre[t] == -1) return false; 53 else return true; 54 } 55 56 int mcmf(int s,int t,int& min_cost){ 57 58 int max_flow = 0,_min = INF; 59 while(spfa(s,t)){ 60 _min = INF; 61 for(int o = pre[t]; ~o; o = pre[e[o^1].to]){ 62 _min = min(_min,e[o].cap - e[o].flow); 63 } 64 for(int o = pre[t]; ~o; o = pre[e[o^1].to]){ 65 e[o].flow += _min; 66 e[o^1].flow -= _min; 67 } 68 min_cost += dis[t]*_min; 69 max_flow += _min; 70 } 71 return max_flow; 72 } 73 74 int main(){ 75 76 int u,v,cap,cost,min_cost = 0; 77 scanf("%d%d%d%d",&n,&m,&s,&t); 78 for(int i = 1; i <= n; ++i) head[i] = -1; tot = 0; 79 for(int i = 1; i <= m; ++i){ 80 scanf("%d%d%d%d",&u,&v,&cap,&cost); 81 add(u,v,cap,cost,0); 82 add(v,u,0,-cost,0); 83 } 84 cout << mcmf(s,t,min_cost) << " " << min_cost << endl; 85 86 return 0; 87 }