#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string.h>
#include <vector>
#include <queue>
#define inf 100000000
#define N 250
using namespace std; //poj1273 hdu1532 网络流(最大流---Edmonds-Karp算法)
int map[N][N], flow[N][N], a[N], p[N]; //flow[u][v]表示从u到v的流量,a记录的是s->e这条线路中的最小残余量的值, p记录的是节点的父节点值(逆推时用到)
int EK(int s, int e) //s为起点,e为终点
{
queue<int> q;
int t, u, f=0;
memset(flow, 0, sizeof(flow));
memset(p, 0, sizeof(p));
while(1)
{
q.push(s);
memset(a, 0, sizeof(a));
a[s]=inf;
while(!q.empty()) //计算出从s到e的一条可行路径
{
u=q.front();
q.pop();
for(t=1; t<=e; ++t)
{
if(!a[t]&&map[u][t]>flow[u][t])
{
p[t]=u;
a[t]=(a[u]<map[u][t]-flow[u][t]?a[u]:map[u][t]-flow[u][t]);
q.push(t);
}
}
}
if(a[e]==0)break; //没有残余容量,当前流已经是最大流
for(t=e; t!=s; t=p[t]) //更新从s到t的一条支路的流量
{
flow[t][p[t]]-=a[e];
flow[p[t]][t]+=a[e];
}
f+=a[e];
}
return f;
}
int main()
{
int n, m, t, b, c, d;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(map, 0, sizeof(map));
for(t=0; t<n; ++t)
{
scanf("%d%d%d", &b, &c, &d);
map[b][c]+=d; //防止重边
}
printf("%d\n", EK(1, m));
}
return 0;
}
网络流(最大流---Edmonds-Karp算法)
最新推荐文章于 2019-12-12 17:58:05 发布