All pastes #2127356 Raw Edit

Someone

public text v1 · immutable
#2127356 ·published 2012-03-12 15:15 UTC
rendered paste body
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>

using namespace std;

#define MAX 110
const int inf = 2000000000;

int n;
int s,t,c;
int cap[MAX][MAX];
int flow[MAX][MAX];
int que[MAX];
int pre[MAX];
int d[MAX];
int p,q;
int a,b,cc;

int main(){
	int networkCnt = 1;
	while (scanf("%d",&n) && n != 0){
		scanf("%d%d%d",&s,&t,&c);
		s--,t--;
		for (int i = 0; i < c; i++){
			scanf("%d%d%d",&a,&b,&cc);a--,b--;
			cap[a][b] = cap[b][a] = cc;
		}
		memset(flow, 0, sizeof flow);
		int res = 0;
		while (true){
			p = q = 0;
			memset(pre, -1, sizeof pre);
			que[q++] = s;
			pre[s] = -2;
			d[s] = inf;
			while (p < q){
				
				int curV = que[p];p++;
				//printf("curV=%d\n",curV);
				for (int nextV = 0; nextV < n; nextV++){
					if (pre[nextV] == -1 && cap[curV][nextV] - flow[curV][nextV] > 0){
						que[q++] = nextV;
						pre[nextV] = curV;
						d[nextV] = min(d[curV], cap[curV][nextV] - flow[curV][nextV]);
					}
				}
			}
			
			if (pre[t] == -1) break;
			
			int amt = d[t];
			//printf("send %d flow\n",amt);
			int v = t;
			res += amt;
			while (v != s){
				flow[ pre[v] ][ v ] += amt;
				flow[ v ][ pre[v] ] -= amt;
				v = pre[v];
				//printf("v is %d\n",v);
			}
		}
		printf("Network %d\nThe bandwidth is %d.\n",networkCnt++,res);
	}
	return 0;
}