题意:给出n个二维坐标系上的点(其实就是n个点),最左的是起点,最右的是终点,且各只有一个,问从起点到终点的最大流。
建边直接上模板即可
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define asd puts("sdfsdfsdfsdfsdfsdf");
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 100010;
struct node{
int v, w, nxt;
}e[N<<2];
int head[N];
int cur[N];
int gap[N];
int s[N], top;
int dep[N];
int st, ed;
int n, m, cnt;
queue <int> q;
void init()
{
cnt = 0;
memset( head, -1, sizeof( head ) );
}
void add( int u, int v, int w )
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].nxt = head[u];
head[u] = cnt++;
e[cnt].v = u;
e[cnt].w = w;
e[cnt].nxt = head[v];
head[v] = cnt++;
}
void rev_bfs()
{
memset( gap, 0, sizeof( gap ) );
memset( dep, -1, sizeof( dep ) );
while( !q.empty() ) q.pop();
q.push(ed);
dep[ed] = 0;
gap[0] = 1;
while( !q.empty() ) {
int u = q.front();
q.pop();
for( int i = head[u]; ~i; i = e[i].nxt ) {
int v = e[i].v;
if( ~dep[v] )
continue;
dep[v] = dep[u] + 1;
gap[dep[v]]++;
q.push(v);
}
}
}
int isap()
{
int flow = 0, u = st, i;
memcpy( cur, head, sizeof head );
rev_bfs();
top = 0;
//int nv = ed + 1;
while( dep[st] < n ) {
if( u == ed ) {
int tmp, minn = inf;
for( i = 0; i < top; ++i ) {
if( minn > e[s[i]].w ) {
minn = e[s[i]].w;
tmp = i;
}
}
for( i = 0; i < top; ++i ) {
e[s[i]].w -= minn;
e[s[i]^1].w += minn;
}
flow += minn;
top = tmp;
u = e[s[top]^1].v;
}
for( i = cur[u]; ~i; i = e[i].nxt ) {
if( e[i].w > 0 && dep[u] == dep[e[i].v] + 1 ) {
cur[u] = i;
break;
}
}
if( ~i ) {
s[top++] = cur[u];
u = e[i].v;
}
else {
if( 0 == (--gap[dep[u]]) )
break;
int minn = n;
for( i = head[u]; ~i; i = e[i].nxt ) {
int v = e[i].v;
if( e[i].w > 0 && minn > dep[v] ) {
minn = dep[v];
cur[u] = i;
}
}
dep[u] = minn + 1;
gap[dep[u]]++;
if( u != st ) {
u = e[ s[--top]^1 ].v;
}
}
}
return flow;
}
int main()
{
int tot;
scanf("%d", &tot);
while( tot-- ) {
int t1 = inf, t2 = -inf; ///t1 for st, t2 for ed
init();
scanf("%d%d", &n, &m);
int x, y;
for( int i = 1; i <= n; ++i ) {
scanf("%d%d", &x, &y);
if( x < t1 ) {
st = i;
t1 = x;
}
if( x > t2 ) {
t2 = x;
ed = i;
}
}
int u, v, w;
while( m-- ) {
scanf("%d%d%d", &u, &v, &w);
add( u, v, w );
}
int ans = isap();
printf("%d\n", ans);
}
return 0;
}