您的位置:首页 > 理论基础 > 计算机网络

hdu 3549 Flow Problem 基本网络流。

2011-08-08 09:30 141 查看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <deque>

using namespace std;

int cap[100][100];
int a[100];
int flow[100][100];
int path[1010];
int N,M;
const int inf = 0x7f7f7f7f;
int f;

void BFS( )
{
int i, j, k, t, u, v;
f = 0;
memset(flow, 0, sizeof(flow));
for (; ; )
{
memset(a, 0, sizeof(a));
memset(path, 0, sizeof(path));
deque<int>q;
a[1] = inf;
q.push_back(1);
while (!q.empty( ))
{
t = q.front( );
q.pop_front( );
for (i = 0; i <= N; i++)
if (!a[i] && flow[t][i] < cap[t][i] )
{
path[i] = t;
a[i] = min(a[t], cap[t][i] - flow[t][i]);
q.push_back(i);
}
}
if (a
== 0)
break;
for (u = N; u != 1; u = path[u])
{
flow[path[u]][u] += a
;
flow[u][path[u]] -= a
;

}
f += a
;
}
printf("%d\n",f);
}

int main( )
{
int T, l = 0, i, b, c, d;
scanf("%d", &T);
while (T--)
{
l++;
scanf("%d%d", &N, &M);
memset(cap, 0, sizeof(cap));
for (i = 0; i < M; i++) {
scanf("%d%d%d", &d, &b, &c);
cap[d][b] += c;
}
printf("Case %d: ",l);
BFS( );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: