Question:
Link: Click to open the link
Question:
Calculate the maximum flow rate.
Ideas:
Edmond_karp.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;#define INF 100000000const int N = 220;int cap[N][N],flow[N][N];int a[N];int n,m;int s,e,c;void edmonds_karp(int s,int t){ queue<int> q; memset(flow,0,sizeof(flow)); int f = 0; int p[N]; for(;;) { memset(a,0,sizeof(a)); a[s] = INF; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int v=1; v<=n; v++) { if(!a[v] && cap[u][v] > flow[u][v]) { p[v] = u; q.push(v); a[v] = a[u] < cap[u][v] - flow[u][v] ? a[u] : cap[u][v] - flow[u][v]; } } } if(a[t] == 0) break; for(int u=t; u!=s; u=p[u]) { flow[p[u]][u] += a[t]; flow[u][p[u]] += a[t]; } f += a[t]; } printf("%d\n",f);}int main(){ //freopen("input.txt","r",stdin); while(scanf("%d%d",&n,&m) != EOF) { memset(cap,0,sizeof(cap)); for(int i=0; i<n; i++) { scanf("%d%d%d",&s,&e,&c); cap[s][e] += c; } edmonds_karp(1,m); } return 0;}
--------------------------------------------------------------------------------
Gains:
------> Augmented Path Algorithm, edmond_karp template:
queue<int> q; memset(flow,0,sizeof(flow)); int f = 0; int p[N]; for(;;) { memset(a,0,sizeof(a)); a[s] = INF; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int v=1; v<=n; v++) { if(!a[v] && cap[u][v] > flow[u][v]) { p[v] = u; q.push(v); a[v] = a[u] < cap[u][v] - flow[u][v] ? a[u] : cap[u][v] - flow[u][v]; } } } if(a[t] == 0) break; for(int u=t; u!=s; u=p[u]) { flow[p[u]][u] += a[t]; flow[u][p[u]] += a[t]; } f += a[t]; }
------------------------------------------------------------------
Fighting, never shrinking; fighting, never stopping ~~~~~~~~~~~~~