题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; const int maxn = ;
const int maxe = ;
const int INF = 0x3f3f3f; struct Edge{
int v,flow,cap;
int next;
Edge(int v=,int flow=,int cap=,int next=):
v(v) , flow(flow), cap(cap), next(next){} }; struct Dinic{
Edge edges[maxe];
int head[maxn],cnt;
int cur[maxn];
int d[maxn];
int vis[maxn];
int s,t; void init(){
memset(head,-,sizeof(head));
cnt = ;
} void addedge(int u,int v,int cap){
edges[cnt] = Edge(v,,cap,head[u]);
head[u] = cnt++;
edges[cnt] = Edge(u,,cap,head[v]);
head[v] = cnt++;
} bool bfs(){
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s] = true;
d[s] = ;
while(!Q.empty()){
int u = Q.front(); Q.pop();
for(int i=head[u];i!=-;i=edges[i].next){
Edge& e = edges[i];
if(!vis[e.v] && e.cap > e.flow){
vis[e.v] = true;
d[e.v] = d[u] + ;
Q.push(e.v);
}
}
}
return vis[t];
} int dfs(int u,int res){
if(u == t || res == ) return res;
int flow = ,f;
for(int& i=cur[u];i!=-;i=edges[i].next){
Edge& e = edges[i];
if(d[e.v] == d[u] + && (f = dfs(e.v,min(res,e.cap-e.flow))) > ){
e.flow += f;
edges[i^].flow -= f;
flow += f;
res -= f;
if(res == ) break;
}
}
return flow;
} int MaxFlow(int s_,int t_){
s = s_; t = t_;
int flow = ;
while(bfs()){
for(int i=;i<=maxn;i++) cur[i] = head[i];
flow += dfs(s,INF);
} return flow;
} }solver; int main()
{
freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
while(T--){
solver.init();
int N,M;
cin>>N>>M;
int maxx=-INF,minx=INF;
int s,t;
for(int i=;i<=N;i++){
int x,y;
scanf("%d %d",&x,&y);
if(maxx < x){
maxx = x;
t = i;
}
if(minx > x){
minx = x;
s = i;
}
}
for(int i=;i<=M;i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
solver.addedge(a,b,c);
}
printf("%d\n",solver.MaxFlow(s,t));
}
}