赤裸裸的Ford-Fulkerson算法求最大流。

#include <iostream>
#include
<cstring>
#include
<queue>

using namespace std;
const int MAX = 1003;
int map[MAX][MAX],pre[MAX],n,m,ans;
bool vis[MAX];

int Maxflow()    //  采用BFS找增广路径
{
while(true)
{
queue
<int> que;
memset(pre,
0,sizeof(pre));
memset(vis,
0,sizeof(vis));
vis[
1] = true; que.push(1);

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

if(cur == n) break;  //  表明已找到一条增广路径

for(int i = 1;i <= n;++i)
{
if(!vis[i] && map[cur][i])
{
que.push(i); pre[i]
= cur;
vis[i]
= true;
}
}
}

if(!vis[n]) break;    //  无法找到增广路径

int Min = 100000000;

for(int u = n;u != 1;u = pre[u])
{
if(Min > map[pre[u]][u]) Min = map[pre[u]][u];
}

for(u = n;u != 1;u = pre[u])
{
map[pre[u]][u]
-= Min;
map[u][pre[u]]
+= Min;
}
ans
+= Min;
}

return ans;
}


int main()
{
//freopen("hdu.txt","r",stdin);
int u,v,d,t,i,j;

cin
>>t;

for(int ca = 1;ca <= t;++ca)
{
// cin>>n>>m;
scanf("%d %d",&n,&m);

for(i = 1;i <= n;++i)
{
for(j = 1;j <= n;++j) map[i][j] = 0;
}

for(i = 0;i < m;++i)
{
// cin>>u>>v>>d;
scanf("%d %d %d",&u,&v,&d);
map[u][v]
+= d;
}

ans
= 0;
Maxflow();

cout
<<"Case "<<ca<<": "<<ans<<endl;
}

return 0;
}
posted on 2011-02-24 19:26  c++fans  阅读(626)  评论(0编辑  收藏  举报