poj 1273 最大流入门

时间:2023-02-15 23:49:19

明天再拍一遍

 #include <iostream>
#include <queue>
using namespace std;
const int N = ;
const int INF = 0x7FFFFFFF;
int n,m,map[N][N],path[N],flow[N],start,end;
queue<int> q;
int bfs()
{
int i,t;
while(!q.empty()) q.pop();
memset(path,-,sizeof(path));
path[start]=,flow[start]=INF;
q.push(start);
while(!q.empty())
{
t=q.front();
q.pop();
if(t==end) break;
for(i=;i<=m;i++)
{
if(i!=start && path[i]==- && map[t][i])
{
flow[i]=flow[t]<map[t][i]?flow[t]:map[t][i];
q.push(i);
path[i]=t;
}
}
}
if(path[end]==-) return -;
return flow[m]; //一次遍历之后的流量增量
}
int Edmonds_Karp()
{
int max_flow=,step,now,pre;
while((step=bfs())!=-)
{ //找不到增路径时退出
max_flow+=step;
now=end;
while(now!=start)
{
pre=path[now];
map[pre][now]-=step; //更新正向边的实际容量
map[now][pre]+=step; //添加反向边
now=pre;
}
}
return max_flow;
}
int main()
{
int i,u,v,cost;
while(scanf("%d %d",&n,&m)!=EOF)
{
memset(map,,sizeof(map));
for(i=;i<n;i++)
{
scanf("%d %d %d",&u,&v,&cost);
map[u][v]+=cost; //not just only one input
}
start=,end=m;
printf("%d\n",Edmonds_Karp());
}
return ;
}