最大流问题 需要设一个超级源 超级会点 结点也有容量 所以建图的时候要注意一下
#include<cstdio>
#include<cstring>#include<queue>
using namespace std;
#define M 220
#define INF 1<<27
int n,m,B,D,t;
int head[M],end[M];
int flow[M][M],cap[M][M],a[M],p[M];
int init()
{
memset(cap,0,sizeof(cap));
memset(p,0,sizeof(p));
memset(head,0,sizeof(head));
memset(end,0,sizeof(end));
}
int bfs()
{
queue<int>q;
memset(flow,0,sizeof(flow));
int f = 0;
while(1)
{
memset(a,0,sizeof(a));
a[0] = INF;
q.push(0);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v = 1; v <= 2*n+1; v++)
if(!a[v]&&cap[u][v]>flow[u][v])
{
p[v] = u;
q.push(v);
a[v] = min(a[u],cap[u][v]-flow[u][v]);
}
}
if(!a[t]) break;
for(int u = t; u != 0; u = p[u])
{
flow[p[u]][u] += a[t];
flow[u][p[u]] -= a[t];
}
f += a[t];
}
return f;
}
int main()
{
while(scanf("%d",&n)==1)
{
init();
int c;
int point = 1;
for(int i = 1; i <= n; i++)
{
scanf("%d",&c);
head[i] = point;
end[i] = point+1;
point += 2;
cap[head[i]][end[i]] = c;
}
scanf("%d",&m);
int a,b;
for(int i = 1; i <= m; i++)
{
scanf("%d %d %d",&a,&b,&c);
cap[end[a]][head[b]] = c;
}
scanf("%d %d",&B,&D);
for(int i = 0; i < B; i++)
{
scanf("%d",&c);
cap[0][head[c]] = INF;
}
for(int i = 0; i < D; i++)
{
scanf("%d",&c);
cap[end[c]][point] = INF;
}
t = point;
printf("%d\n",bfs());
}
return 0;
}