Island Transport
time limit per test 10 second memory limit per test 256 megabytes
In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.
You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.

传送门:HDU4280

Input

The first line contains one integer T (1<=T<=20), the number of test cases. Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N. Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000. Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour. It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.

Output

For each test case, output an integer in one line, the transport capacity.

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
5 7
3 3
3 0
3 1
0 0
4 5
1 3 3
2 3 4
2 4 3
1 5 6
4 5 3
1 4 4
3 4 2
6 7
-1 -1
0 1
0 2
1 0
1 1
2 3
1 2 1
2 3 6
4 5 5
5 6 3
1 4 6
2 5 5
3 6 4

Sample Output

1
2
9
6

题解

n,m都有1e5 很奇怪网络流竟然能过这题= = 不管 据说isap是比较快的网络流算法了 先学习一波 还有这题的流量是双向边 其他没什么了
## AC code:(不包含输入类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.io.*;  
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
int t=sc.nextInt();
while(sc.hasNext()){
int n=sc.nextInt();
int m=sc.nextInt();
int windex=-1;
int west=999999;
int eindex=-1;
int east=-999999;
for(int i=1;i<=n;i++){
int x=sc.nextInt();
int y=sc.nextInt();
if(x<west){
west=x;
windex=i;
}
if(x>east){
east=x;
eindex=i;
}
}
Isap isap=new Isap();
for(int i=1;i<=m;i++){
int x=sc.nextInt();
int y=sc.nextInt();
int u=sc.nextInt();
//System.out.println(real[x]+" "+real[y]);
isap.add_undiredge(x, y, u);
}
pw.println(isap.isap(windex, eindex, n));
pw.flush();
}

}
}
class Node{
int x;
int y;
Node(int a,int b){
x=a;
y=b;
}
}
class Isap{
static int MAXN = (int) (1e5 + 7);
static int MAXM = (int) (2e5 + 7); //注意边的数目,因为连边是连两条/经常re
static int INF = 0x3f3f3f3f;
static Edge[]edge=new Edge[MAXM<<1];
static int[]head=new int[MAXN];
static int tot;
static int[]gap=new int[MAXN];
static int[]d=new int[MAXN];
static int[]cur=new int[MAXN];
static int[]que=new int[MAXN];
static int[]p=new int[MAXN];
Isap(){
tot=0;
for(int i=0;i<MAXN<<1;i++)edge[i]=new Edge();
Arrays.fill(head, -1);
}
void add_undiredge(int u, int v, int c)
{
edge[tot].to = v;
edge[tot].cap = c;
edge[tot].flow = 0;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].to = u;
edge[tot].cap = c;
edge[tot].flow = 0;
edge[tot].next = head[v];
head[v] = tot++;
}
void add_diredge(int u, int v, int c)
{
edge[tot].to = v;
edge[tot].cap = c;
edge[tot].flow = 0;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].to = u;
edge[tot].cap = 0;
edge[tot].flow = 0;
edge[tot].next = head[v];
head[v] = tot++;
}
void BFS(int source, int sink)
{
Arrays.fill(d, -1); //clr(d,-1);
Arrays.fill(gap,0); //clr(gap,0);
gap[0] = 1;
int front = 0, rear = 0;
d[sink] = 0;
que[rear++] = sink;
while (front != rear)
{
int u = que[front++];
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (d[v] != -1) continue;
que[rear++] = v;
d[v] = d[u] + 1;
gap[d[v]]++;
}
}
}
int isap(int source, int sink, int N)
{
BFS(source, sink);
//memcpy(cur, head, sizeof(head));
for(int i=0;i<MAXN;i++)cur[i]=head[i];
// cur=Arrays.copyOf(head, MAXN);
int top = 0, x = source, flow = 0;
while (d[source] < N)
{
if (x == sink)
{
int Min = INF, inser = 0;
for (int i = 0; i < top; ++i)
{
if (Min > edge[p[i]].cap - edge[p[i]].flow)
{
Min = edge[p[i]].cap - edge[p[i]].flow;
inser = i;
}
}
for (int i = 0; i < top; ++i)
{
edge[p[i]].flow += Min;
edge[p[i] ^ 1].flow -= Min;
}
flow += Min;
top = inser;
x = edge[p[top] ^ 1].to;
continue;
}
int ok = 0;
for (int i = cur[x]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > edge[i].flow && d[v] + 1 == d[x])
{
ok = 1;
cur[x] = i;
p[top++] = i;
x = edge[i].to;
break;
}
}
if (ok==0)
{
int Min = N;
for (int i = head[x]; i != -1; i = edge[i].next)
{
if (edge[i].cap > edge[i].flow && d[edge[i].to] < Min)
{
Min = d[edge[i].to];
cur[x] = i;
}
}
if (--gap[d[x]] == 0) break;
gap[d[x] = Min + 1]++;
if (x != source) x = edge[p[--top] ^ 1].to;
}
}
return flow;
}
}
class Edge{
int to,next,cap,flow;
}