| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
第一道网络流题mark#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define M 205
int n;
int s, t;
int c[M][M];
int f[M][M];
int p[M];
bool v[M];
bool aug()
{
//memset(p,0,sizeof(p));
queue<int> q;
q.push(s);
memset(v,0,sizeof(v));
v[s] = 1;
while (!q.empty())
{
int qf = q.front(); q.pop();
for (int i = 1; i <= n; ++ i)
{
if (!v[i] && c[qf][i])
{
v[i] = 1;
p[i] = qf;
q.push(i);
if (i == t) return 1;
}
}
}
return 0;
}
int fordFulkerson()
{
int res = 0;
memset(f,0,sizeof(f));
while (1)
{
if (!aug()) break;
int i = t;
int mr = (1<<30);
while (i != s)
{
if (mr > c[p[i]][i])
mr = c[p[i]][i];
i = p[i];
}
i = t;
while (i != s)
{
c[p[i]][i] -= mr;
c[i][p[i]] += mr;
i = p[i];
}
res += mr;
}
return res;
}
int main()
{
int e;
while (~scanf("%d %d", &e, &n))
{
s = 1; t = n;
memset(c,0,sizeof(c));
while (e --)
{
int a1, a2, a3;
scanf("%d %d %d", &a1, &a2, &a3);
c[a1][a2] += a3;
}
printf("%d\n", fordFulkerson());
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator