分析在这里
窝把原来的那个方法也写上好了,反正也差不多....
其实....还写了最高标号预流推进....................................就是这么蛋疼....然而在这道题目上来说,,这三种方法其实都一样。。。
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
using namespace std;
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
typedef pair<int,int> pii;
typedef long long LL;
//------------------------------
const int maxn = 205;
int n,m;
struct Edge{
int to, cap, rev;//终点 容量 反向边
Edge(int to_ = 0, int cap_ = 0, int rev_ = 0){
to = to_;
cap = cap_;
rev = rev_;
}
};
vector<Edge> g[maxn];
int level[maxn];//顶点到源点的距离标号
int iter[maxn];
void add_Edge(int from, int to, int cap){
Edge tmp1(to, cap, g[to].size());
g[from].push_back(tmp1);
Edge tmp2(from, 0, g[from].size()-1);
g[to].push_back(tmp2);
}
void bfs(int s){
memset(level, -1, sizeof(level));
queue<int> q;
level[s] = 0;
q.push(s);
while(!q.empty()){
int v = q.front();
q.pop();
for(int i = 0; i < g[v].size(); i++){
Edge& e = g[v][i];
if(e.cap > 0 && level[e.to] < 0){
level[e.to] = level[v] + 1;
q.push(e.to);
}
}
}
}
int dfs(int v, int t, int f){
if(v == t) return f;
for(int& i = iter[v]; i < g[v].size(); i++){
Edge& e = g[v][i];
if(e.cap > 0 && level[v] < level[e.to]){
int d = dfs(e.to, t, min(e.cap, f));
if(d > 0){
e.cap -= d;
g[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t){
int flow = 0;
for(;;){
bfs(s);
if(level[t] < 0) return flow;
memset(iter, 0, sizeof(iter));
int f;
while((f = dfs(s, t, INF)) > 0){
flow += f;
}
}
}
//------------------上面是最大流模板部分 正确----------------------
int np, nc;
void init(){
for(int i = 0; i < maxn; i++) g[i].clear();
int u, v, w;
char ch;
for(int i = 1; i <= m; i++){
while(scanf("%c", &ch)){
if(ch == '(') break;
}
scanf("%d,%d%c%d",&u,&v,&ch,&w);
add_Edge(u, v, w);
}
for(int i = 1; i <= np; i++){
while(scanf("%c", &ch)){
if(ch == '(') break;
}
scanf("%d%c%d",&u, &ch, &w);
add_Edge(n, u, w);
}
for(int i = 1; i <= nc; i++){
while(scanf("%c", &ch)){
if(ch == '(') break;
}
scanf("%d%c%d",&v, &ch, &w);
add_Edge(v, n+1, w);
}
int ans = max_flow(n, n+1);
printf("%d\n",ans);
}
int main(){
while(scanf("%d%d%d%d",&n,&np, &nc, &m) != EOF){
init();
}
return 0;
}