题目源:https://vjudge.net/problem/HDU-4280
http://acm.hdu.edu.cn/showproblem.php?pid=4280
模板源:https://blog.csdn.net/Adolphrocs/article/details/84368661
https://blog.csdn.net/Adolphrocs/article/details/84779691
题解:每两个点建一条边,处理出最左点为源点,最右为汇点
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=100005;
const int maxx=200005;
int edge;
int to[maxx], flow[maxx], nex[maxx];
int head[maxn];
void addEdge(int v, int u, int cap){
to[edge] = u, flow[edge] = cap, nex[edge] = head[v], head[v] = edge++;
to[edge] = v, flow[edge] = cap, nex[edge] = head[u], head[u] = edge++;
}
int vis[maxn];
int pre[maxn];
bool bfs(int s,int e) {
queue<int> que;
pre[s] = -1;
memset(vis, -1, sizeof(vis));
que.push(s);
vis[s] = 0;
while(!que.empty()) {
int u = que.front();
que.pop();
for(int i = head[u]; ~i; i = nex[i]) {
int v = to[i];
if(vis[v] == -1 && flow[i]) {
vis[v] = vis[u] + 1;
if(v == e)
return true;
que.push(v);
}
}
}
return false;
}
int dfs(int s, int t, int f){
if(s == t || !f)
return f;
int r = 0;
for(int i = head[s]; ~i; i = nex[i]) {
int v = to[i];
if(vis[v] == vis[s] + 1 && flow[i]) {
int d = dfs(v, t, min(f, flow[i]));
if(d > 0) {
flow[i] -= d;
flow[i ^ 1] += d;
r += d;
f -= d;
if(!f)
break;
}
}
}
if(!r)
vis[s] = INF;
return r;
}
int maxFlow(int s, int e) {
int ans = 0;
while(bfs(s, e))
ans += dfs(s, e, INF);
return ans;
}
void init() {
memset(head, -1, sizeof(head));
edge = 0;
}
int main() {
int t;
scanf("%d", &t);
int x, y, w;
int n, m;
int s, e;
int maxI, minI;
while(t--){
init();
scanf("%d%d", &n, &m);
scanf("%d%d", &x, &y);
s = e = 1;
maxI = minI = x;
for(int i = 2; i <= n; i++) {
scanf("%d%d", &x, &y);
if(maxI < x)
e = i, maxI = x;
if(minI > x)
s = i, minI = x;
}
while(m--) {
scanf("%d%d%d", &x, &y, &w);
addEdge(x, y, w);
}
int ans = maxFlow(s, e);
printf("%d\n", ans);
}
return 0;