PS:注意是双向边
代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mod 1000000007
#define lowbit(x) (x&(-x))
#define mem(a,b) memset(a,b,sizeof(a))
#define FRER() freopen("in.txt","r",stdin);
#define FREW() freopen("out.txt","w",stdout);
using namespace std;
typedef pair<int,int> pii;
const int maxn = 100 + 7 , inf = 0x3f3f3f3f ;
int G[maxn][maxn], pre[maxn],vis[maxn];
int n,k;
void update(int u,int v){
while(pre[u]!=-1){
G[pre[u]][u] -= v;
G[u][pre[u]] += v;
u = pre[u];
}
}
int bfs(int s,int t){
mem(vis,0);
mem(pre,-1);
vis[s] = 1;
int Min = inf;
queue<int>q;
q.push(s);
while(!q.empty()){
int now = q.front();q.pop();
if(now==t)break;
for(int i=1;i<=n;i++){
if(!vis[i]&&G[now][i]!=0){
q.push(i);
Min = min(Min,G[now][i]);
pre[i] = now;
vis[i] = 1;
}
}
}
if(pre[t]==-1)return 0;
return Min;
}
int EK(int s,int t){
int new_flow = 0;
int max_flow = 0;
do{
new_flow = bfs(s,t);
update(t,new_flow);
max_flow += new_flow;
}while(new_flow!=0);
return max_flow;
}
int main(){
//FRER();
//FREW();
k = 1;
while(scanf("%d",&n)==1&&n){
mem(G,0);
int s,t,m;
scanf("%d%d%d",&s,&t,&m);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
G[a][b]+=c;
G[b][a]+=c;
}
printf("Network %d\n",k++);
printf("The bandwidth is %d.\n\n",EK(s,t));
}
}