#include "stdio.h" //poj 1459 最大流 #include "string.h" #include "queue" using namespace std; #define N 205 #define INF 0x3fffffff int n; bool mark; int map[N][N],maxf[N],route[N]; int EK(); int BFS(); void init(); int MIN(int x,int y); int main() { int i,j; char ch; int m,np,nc; int u,v,w; while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=-1) { init(); while(m--) { scanf(" (%d,%d)%d",&u,&v,&w); if(u==v) continue; map[u+1][v+1] += w; } int start = 0; //超级源点 while(np--) { scanf(" (%d)%d",&v,&w); map[start][v+1] += w; } int end = n+1; //超级汇点 while(nc--) { scanf(" (%d)%d",&u,&w); map[u+1][end] += w; } int ans = EK(); printf("%d ",ans); } return 0; } void init(){ memset(map,0,sizeof(map)); } int MIN(int x,int y) { return x<y?x:y; } int EK() { int ans=0,kejia; int x,y; while(kejia = BFS()) { ans += kejia; y = n+1; while(y!=0) { x = route[y]; map[x][y]-=kejia; map[y][x]+=kejia; y = x; } } return ans; } int BFS() { int i; int x,y; memset(route,-1,sizeof(route)); for(i=0;i<N;i++) maxf[i] = INF; queue<int> q; route[0] = 0; q.push(0); while(!q.empty()) { x = q.front(); q.pop(); for(y=0;y<=n+1;y++) { if(route[y]==-1 && map[x][y]!=0) { maxf[y] = MIN(maxf[x],map[x][y]); route[y] = x; q.push(y); } } } if(route[n+1]==-1) return 0; return maxf[n+1]; }