比较简单的一道题目,考察的是最大流算法,注意在建立逆向的流路的时候容量是设置为和顺向的流路一样的大小即可,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;
class Edge{
public:
int from, to, cap, flow;
Edge(int fr, int t, int c, int fl) :from(fr), to(t), cap(c), flow(fl){}
};
class Solve{
public:
int n;
int s, t, c;
vector<Edge> edge;
vector<int> G[110];
void addEdge(int from,int to,int cap){
edge.push_back(Edge(from,to,cap,0));
edge.push_back(Edge(to, from, cap, 0));
int m = edge.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
int MaxFlow(){
int flow = 0;
while (true){
int Flow[110];
int parent[110];
memset(Flow, 0, sizeof(Flow));
Flow[s] = 1 << 30;
queue<int> q;
q.push(s);
while (!q.empty()){
int id = q.front();
q.pop();
for (int i = 0; i < G[id].size(); i++){
int ide = G[id][i];
int to = edge[ide].to;
if (!Flow[to]&&edge[ide].cap > edge[ide].flow){
parent[to] = ide;
Flow[to] = min(Flow[id],edge[ide].cap-edge[ide].flow);
q.push(to);
}
}
if (Flow[t]) break;
}
if (!Flow[t]) break;
flow += Flow[t];
for (int i = t; i != s; i = edge[parent[i]].from){
edge[parent[i]].flow += Flow[t];
edge[parent[i] ^ 1].flow -= Flow[t];
}
}
return flow;
}
void Init(){
edge.clear();
for (int i = 0; i < 110; i++) G[i].clear();
cin >> s >> t >> c;
for (int i = 0; i < c; i++){
int from, to, cap;
cin >> from >> to >> cap;
addEdge(from,to,cap);
}
}
int Deal(){
Init();
return MaxFlow();
}
};
int main(){
Solve a;
int Case = 0;
while (cin >> a.n){
Case++;
if (a.n == 0) break;
int res = a.Deal();
cout << "Network " << Case << endl;
cout << "The bandwidth is " << res << "." << endl << endl;;
}
return 0;
}