题意:
思路:
//By SiriusRen
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int N=20005,M=880005;
int first[N],vis[N],next[M],v[M],w[M],tot,xx,yy,zz,n,m,T,ans;
void Add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void add(int x,int y,int z){Add(x,y,z),Add(y,x,0);}
bool tell(){
queue<int>q;q.push(0);
memset(vis,-1,sizeof(vis)),vis[0]=0;
while(!q.empty()){
int t=q.front();q.pop();
for(int i=first[t];~i;i=next[i])if(!~vis[v[i]]&&w[i])
vis[v[i]]=vis[t]+1,q.push(v[i]);
}return ~vis[T];
}
int zeng(int x,int y){
if(x==T)return y;
int r=0;
for(int i=first[x];~i&&y>r;i=next[i])
if(vis[v[i]]==vis[x]+1&&w[i]){
int t=zeng(v[i],min(y-r,w[i]));
w[i]-=t,w[i^1]+=t,r+=t;
}
if(!r)vis[x]=-1;
return r;
}
int main(){
mem(first,-1),scanf("%d%d",&n,&m),T=n+1;
for(int i=1;i<=n;i++)scanf("%d%d",&xx,&yy),add(0,i,xx),add(i,T,yy);
for(int i=1;i<=m;i++)scanf("%d%d%d",&xx,&yy,&zz),add(xx,yy,zz),add(yy,xx,zz);
while(tell())while(xx=zeng(0,0x3f3f3f3f))ans+=xx;
printf("%d\n",ans);
}