poj 1459 超级源点超级汇点EK最大流

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 100 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);

int n, np, nc, m;
int cap[maxn][maxn];
int a[maxn];//残量
int flow[maxn][maxn];//流量
int p[maxn];//父节点

void Init()
{
memset(cap, 0, sizeof(cap));
//    memset(a, 0, sizeof(a));
memset(flow, 0, sizeof(flow));
memset(p, 0, sizeof(p));
}

int EK(int s, int t)
{
queue<int> q;
int res = 0;
while (1)
{
memset(a, 0, sizeof(a));
a[s] = inf;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = s; v <= t; v++)
{
if (!a[v] && flow[u][v] < cap[u][v])
{
p[v] = u;
a[v] = min(a[u], cap[u][v] - flow[u][v]);
q.push(v);
}
}
}
if (a[t] == 0)
return res;
for (int u = t; u != s; u = p[u])
{
flow[p[u]][u] += a[t];
flow[u][p[u]] -= a[t];
}
res += a[t];
}
}

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
while (scanf("%d%d%d%d", &n, &np, &nc, &m) != EOF)
{
Init();
for (int i = 0; i < m; i++)
{
int fr, to, z;
scanf(" (%d,%d)%d", &fr, &to, &z);
cap[fr + 1][to + 1] += z;///!!!!!! +=........
}
for (int i = 0; i < np; i++)
{
int power, z;
scanf(" (%d)%d", &power, &z);
cap[0][power + 1] += z;
}
for (int i = 0; i < nc; i++)
{
int consumer, z;
scanf(" (%d)%d", &consumer, &z);
cap[consumer + 1][n + 1] += z;
}
int s = 0, t = n + 1;
printf("%d\n", EK(s, t));
}
return 0;
}