原题链接: ​​http://acm.hdu.edu.cn/showproblem.php?pid=1532​


题意:

n条路,m个点,每条边一个流速,求最大流速,1是源点,m是汇点。

依旧模板题。

#define _CRT_SECURE_NO_DEPRECATE 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>

using namespace std;

int n, m;
int s, t;
int pre[205];
int vis[205];
int p[205][205];

bool bfs()
{
memset(pre, 0, sizeof(pre));
memset(vis, 0, sizeof(vis));
vis[s] = 1;
queue<int> Q;
Q.push(s);

while (!Q.empty())
{
int cur = Q.front();
Q.pop();

if (cur == t)
return true;

for (int i = 1; i <= n; i++)
{
if (!vis[i] && p[cur][i])
{
pre[i] = cur;
vis[i] = 1;
Q.push(i);
}
}
}
return false;
}

int maxFlow()
{
int ans = 0;
while (1)
{
if (!bfs())
return ans;
int minn = 99999999;
for (int i = t; i != s; i = pre[i])
minn = min(minn, p[pre[i]][i]);
for (int i = t; i != s; i = pre[i])
{
p[pre[i]][i] -= minn;
p[i][pre[i]] += minn;
}
ans += minn;
}
}

int main()
{
int u, v, w;
while (~scanf("%d%d", &m, &n))
{
s = 1;
t = n;
memset(p, 0, sizeof(p));//注意这里要初始化

for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
p[u][v] += w;
}

printf("%d\n", maxFlow());
}
return 0;
}