题意:其实就是给定一个无向图,给出每条边的最大流量,问从起点到终点的最大流量。
赤裸裸的最大流。
由于边无向,添加的时候当成正反两条有向边添加即可。
然后就是跑一下最大流的模板。
PS:每组输出后面要跟一个空行,如果少了OJ上面没有PE,会直接判WA。
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 110;
const int inf = 0x7fffffff;
#define pb push_back
struct Edge{
int from, to, cap, flow;
Edge(){}
Edge(int from, int to, int cap, int flow):from(from),to(to),cap(cap),flow(flow){}
};
int n, m, s, t;
int d[N], cur[N];
vector<Edge> G;
vector<int> V[N];
void add(int from, int to, int cap){
G.pb(Edge(from,to,cap,0));
G.pb(Edge(to,from,0,0));
int x = G.size();
V[from].pb(x-2);
V[to].pb(x-1);
}
bool bfs(){
memset(d, -1, sizeof(d));
d[s] = 0;
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int x=Q.front(); Q.pop();
for(int i=0; i<V[x].size(); i++){
Edge &e = G[V[x][i]];
if(d[e.to]==-1 && e.cap>e.flow){
d[e.to] = d[x]+1;
Q.push(e.to);
}
}
}
return d[t]!=-1;
}
int dfs(int x, int v){
if(x==t || v==0) return v;
int flow=0, f;
for(int &i=cur[x]; i<V[x].size(); i++){
int j = V[x][i];
Edge &e = G[j];
if(d[e.to]==d[x]+1 && (f=dfs(e.to, min(v, e.cap-e.flow)))>0){
flow += f;
v -= f;
e.flow+=f;
G[j^1].flow-=f;
if(!v) break;
}
}
return flow;
}
int max_flow(){
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow += dfs(s, inf);
}
return flow;
}
int main(){
int ct=0;
while(~scanf("%d", &n) && n){
scanf("%d %d %d", &s, &t, &m);
for(int i=1; i<=n; i++) V[i].clear();
G.clear();
int a, b, c;
while(m--){
scanf("%d %d %d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
printf("Network %d\nThe bandwidth is %d.\n\n", ++ct, max_flow());
}
return 0;
}