int main(void) { int N,M,res; int u,v,c,a,b; /* 0, N+1 represent CoreA, CoreB respectively */ scanf("%d%d",&N,&M); V=N+2; memset(hdr, -1, sizeof(hdr[0])*V); for(v=1;v<=N;v++) { scanf("%d%d",&a,&b); connect(v, N+1, a); connect(N+1, v, 0); connect(0, v, b); connect(v, 0, 0); } while(M--) { scanf("%d%d%d",&u,&v,&c); connect(u, v, c); connect(v, u, c); } res=0; while(dinicBFS()) res += dinicDFS(); printf("%d\n", res); return 0; }
int maxflow(int s, int t){ this->s = s, this->t = t; int flow=0, mf; while ( dinicBFS() ){ memset(cur,0,sizeof(cur)); while ( (mf=dinicDFS(s,INF)) )flow+=mf; } return flow; }
int dinicDFS(int u, int a){ if (u==t || a==0)return a; int flow=0, f; for (int &i=cur[u]; i<(int)G[u].size(); i++){ Edge &e = edges[ G[u][i] ]; if (d[u]+1!=d[e.to])continue; f = dinicDFS(e.to, min(a, e.cap-e.flow) ); if (f>0){ e.flow += f; edges[ G[u][i]^1 ].flow -=f; flow += f; a -= f; if (a==0)break; } } return flow; }