#include<stdio.h> #define Max_vertex 10240 #define Max_Edge 1024000 #define Inf 0x7fffffff int To_Node[Max_Edge],Start_Node[Max_Edge],next[Max_Edge],Capacity[Max_Edge]; int Start[Max_vertex],Label[Max_vertex],Queue[Max_vertex]; int Source,Sink,Node_Num,Edge_Init,N,M; int min(int x,int y){ if(x<y) return x; else return y; } void __insert(int from,int to ,int capacity){//initialize every edge with its capacity Edge_Init++; Capacity[Edge_Init]=capacity; To_Node[Edge_Init]=to; Start_Node[Edge_Init]= from; next[Edge_Init]=Start[from]; Start[from]=Edge_Init; } void insert(int from,int to,int capacity){//initialize every edge and its reverse edge __insert(from,to,capacity); __insert(to,from,0); } int breath_first_seach(){//find shortest path and label every layer int Top,Tail,edge,u,v,i; Top=Tail=0; Queue[Tail++]=Source; for(i=0; i<=Node_Num; i++) Label[i] =0; Label[Source]=1; while(Top<Tail){ u=Queue[Top++]; for(edge=Start[u]; edge!=-1; edge=next[edge]){ v=To_Node[edge]; if(Label[v]==0 && Capacity[edge]>0){ Label[v]=Label[u]+1; Queue[Tail++]=v; } } } return Label[Sink]; } int depth_first_search(int u,int flow){//find an augmenting path if( u == Sink) return flow; int t,edge,v, nowflow = 0; for(edge = Start[u]; edge!=-1 && nowflow<flow; edge=next[edge]){ v = To_Node[edge]; if(Capacity[edge]>0 && Label[v] == Label[u] + 1){ if(t = depth_first_search(To_Node[edge], min(Capacity[edge],flow-nowflow))){ Capacity[edge] -= t; Capacity[edge^1] += t; nowflow += t; } } } if(!nowflow) Label[u] = -1; return nowflow; } int dinic(){//Use dinic to solve Maximum flow problem int flow=0, nowflow; while(breath_first_seach()){ nowflow = depth_first_search(Source,Inf); flow = flow + nowflow; } return flow; } void init(int all,int from,int to){//Initialize Start & End, and very edge int i; Edge_Init=-1; Node_Num=all; Source=from;Sink=to; for( i=0; i<=Node_Num; i++) Start[i] =-1; } int main(){ int i,j,k,x,y,z; scanf("%d%d", &N,&M); init(N+4,0,N+2); for(i=1; i<=N; i++){ scanf("%d%d",&x,&y); insert(Source,i,x); insert(i,Sink,y); } for(i=0; i<M;i++){ scanf("%d%d%d", &x,&y,&z); insert(x,y,z); insert(y,x,z); } int ans=dinic(); printf("%d\n",ans); }