[poj1273]Drainage Ditches(最大流)

时间:2021-04-15 07:19:49

解题关键:最大流裸题

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<queue>
#include<vector>
#define inf 0x3f3f3f3f
#define MAX_V 252
using namespace std;
typedef long long ll;
struct edge{int to,cap,rev;};//终点,容量,反向边
vector<edge>G[MAX_V];
int level[MAX_V],iter[MAX_V];
int n,m,s,t;
void add_edge(int from,int to,int cap){
G[from].push_back((edge){to,cap,(int)G[to].size()});
G[to].push_back((edge){from,,(int)G[from].size()-});
}
void bfs(int s){
memset(level,-,sizeof level);
queue<int>que;
level[s]=;
que.push(s);
while(!que.empty()){
int v=que.front();que.pop();
for(int i=;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>&&level[e.to]<){
level[e.to]=level[v]+;
que.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>&&level[v]<level[e.to]){
int d=dfs(e.to,t,min(f,e.cap));
if(d>){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
} int dicnic(int s,int t){
int flow=,f;
while(){
bfs(s);
if(level[t]<) return flow;
memset(iter,,sizeof iter);
while((f=dfs(s,t,inf))>){
flow+=f;
}
}
return flow;
} int main(){
int u,v,f;
while(scanf("%d%d",&m,&n)!=EOF){
memset(G,,sizeof G);
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&f);
add_edge(u,v,f);
}
s=,t=n;
int ans=dicnic(s,t);
printf("%d\n",ans);
}
return ;
}