1、题目类型:图论、最大流、Edmonds_Karp算法。
2、解题思路:简单最大流问题,Edmonds_Karp算法的经典应用。
3、注意事项:对比Ford_Fulkerson算法DFS寻找增广路径,Edmonds_Karp算法BFS寻找增广路径效率更高。
4、实现方法:
#include < iostream >
#include < cmath >
#include < queue >
using namespace std;
#define Max 110
#define INF 99999999
int n,np,nc,m,s,t;
int map[Max][Max],tmp[Max],pre[Max];
void Init()
{
int i,u,v,z;
char ch;
memset(map, 0 , sizeof (map));
cin >> np >> nc >> m;
for (i = 0 ;i < m;i ++ )
{
cin >> ch >> u >> ch >> v >> ch >> z;
map[u + 1 ][v + 1 ] = z;
}
for (i = 0 ;i < np;i ++ )
{
cin >> ch >> u >> ch >> z;
map[ 0 ][u + 1 ] = z;
}
for (i = 0 ;i < nc;i ++ )
{
cin >> ch >> u >> ch >> z;
map[u + 1 ][n + 1 ] = z;
}
}
int BFS()
{
int i,k;
queue < int > Q;
memset(tmp, 0 , sizeof (tmp));
memset(pre, 0 , sizeof (pre));
tmp[s] = INF;
Q.push(s);
while ( ! Q.empty())
{
k = Q.front();
Q.pop();
for (i = 0 ;i <= t;i ++ )
{
if (tmp[i] == 0 && map[k][i])
{
tmp[i] = tmp[k] > map[k][i] ? map[k][i]:tmp[k];
pre[i] = k;
Q.push(i);
}
}
}
if (tmp[t] == 0 )
return - 1 ;
return 1 ;
}
void Edmonds_Karp()
{
int i,ans = 0 ;
s = 0 ,t = n + 1 ;
while (BFS() != - 1 )
{
ans += tmp[t];
for (i = t;i != s;i = pre[i])
{
map[pre[i]][i] -= tmp[t];
map[i][pre[i]] += tmp[t];
}
}
cout << ans << endl;
}
int main()
{
while (cin >> n)
{
Init();
Edmonds_Karp();
}
return 1 ;
}