UVA_10030

对于题目中每个点的容量的限制,我们可以把一个点i拆成两个点2*i2*i ^ 1,并连一条有向边2*i->2*i^1,容量为该点的容量限制,在建图的时候从2*i流入并从2*i^1流出即可。

#include<stdio.h>
#include<string.h>
#define MAXD 210
#define MAXM 81000
#define INF 100000000
int N, flow[MAXM], first[MAXD], next[MAXM], u[MAXM], v[MAXM], e;
int work[MAXD], d[MAXD], q[MAXD];
int add(int a, int b, int w)
{
u[e] = a;
v[e] = b;
flow[e] = w;
next[e] = first[a];
first[a] = e;
e ++;
}
int init()
{
int i, j, a, b, w, M, B, D;
if(scanf("%d", &N) != 1)
return 0;
e = 0;
memset(first, -1, sizeof(first));
for(i = 1; i <= N; i ++)
{
scanf("%d", &w);
add(2 * i, 2 * i ^ 1, w);
add(2 * i ^ 1, 2 * i, 0);
}
scanf("%d", &M);
for(i = 0; i < M; i ++)
{
scanf("%d%d%d", &a, &b, &w);
add(2 * a ^ 1, 2 * b, w);
add(2 * b, 2 * a ^ 1, 0);
}
scanf("%d%d", &B, &D);
for(i = 0; i < B; i ++)
{
scanf("%d", &j);
add(0, 2 * j, INF);
add(2 * j, 0, 0);
}
for(i = 0; i < D; i ++)
{
scanf("%d", &j);
add(2 * j ^ 1, 1, INF);
add(1, 2 * j ^ 1, 0);
}
return 1;
}
int bfs()
{
int i, j, rear;
memset(d, -1, sizeof(d));
d[0] = 0;
rear = 0;
q[rear ++] = 0;
for(i = 0; i < rear; i ++)
for(j = first[q[i]]; j != -1; j = next[j])
if(d[v[j]] == -1 && flow[j])
{
d[v[j]] = d[q[i]] + 1;
if(v[j] == 1)
return 1;
q[rear ++] = v[j];
}
return 0;
}
int dfs(int cur, int a)
{
if(cur == 1)
return a;
for(int &i = work[cur]; i != -1; i = next[i])
if(flow[i] && d[v[i]] == d[cur] + 1)
if(int t = dfs(v[i], a < flow[i] ? a : flow[i]))
{
flow[i] -= t;
flow[i ^ 1] += t;
return t;
}
return 0;
}
int dinic()
{
int res = 0, t;
while(bfs())
{
memcpy(work, first, sizeof(first));
while(t = dfs(0, INF))
res += t;
}
return res;
}
int main()
{
while(init())
{
int res = dinic();
printf("%d\n", res);
}
return 0;
}


posted on 2011-10-14 14:32  Staginner  阅读(351)  评论(0编辑  收藏  举报