HDU 4280 island transport 37th ACM/ICPC Tianjin Network Competition 1003 questions (maximum flow template question, efficient sap template)

Source: Internet
Author: User
Island Transport

Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 1296 accepted submission (s): 421

Problem description in the vast waters far away, there are using 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 before t 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 your passengers depart from the specified most island and wocould 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.

 

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 already most and only one island is easternmost. no two islands wowould 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 input2 5 7 3 3 0 3 1 0 4 5 1 3 3 3 3 3 3 4 4 3 1 5 4 5 3 4 4 4 3 4 2 6 7-1- 1 0 1 0 2 1 1 1 1 2 3 1 2 2 2 3 3 6 4 5 5 5 6 3 1 4 6 2 5 3 6 4

 

Sample output9 6

 

Source2012 ACM/ICPC Asia Regional Tianjin online

 

Recommendliuyiding is the Ek that was used by the largest stream before and cannot be used for data that is relatively large. Learned the maximum SAP stream Algorithm . Template question:
 /* Maximum Flow template SAP  */  # Include <Stdio. h> # Include < String . H> # Include <Algorithm> # Include <Iostream> Using   Namespace  STD;  Const   Int Maxn = 100010 ; // Maximum number of points  Const   Int Maxm = 400010 ; //  Maximum number of Edges  Const   Int INF = 0x3f3f3f  ;  Struct  Node {  Int   From  , To, next;  Int CAP;} edge [maxm];  Int  Tol;  Int  Head [maxn];  Int  Dep [maxn];  Int Gap [maxn]; //  Gap [x] = Y: indicates the number of Dep [I] = X in the residual network is Y.  Int N; //  N is the total number of points, including the source and sink points.  Void  Init () {Tol =0  ; Memset (Head, - 1 , Sizeof  (Head ));}  Void Addedge ( Int U, Int V, Int  W) {edge [tol].  From = U; edge [tol]. = V; edge [tol]. Cap = W; edge [tol]. Next =Head [u]; head [u] = Tol ++ ; Edge [tol].  From = V; edge [tol]. = U; edge [tol]. Cap = 0  ; Edge [tol]. Next = Head [v]; head [v] = Tol ++ ;}  Void BFS ( Int Start, Int  End) {memset (DEP, -1 , Sizeof  (DEP); memset (gap,  0 , Sizeof  (GAP); Gap [  0 ] = 1  ;  Int  Que [maxn];  Int  Front, rear; front = Rear = 0  ; Dep [end] =0  ; Que [rear ++] = End;  While (Front! = Rear ){  Int U = que [Front ++ ];  If (Front = maxn) Front = 0  ;  For ( Int I = head [u]; I! =- 1 ; I = Edge [I]. Next ){ Int V = Edge [I].;  If (DEP [v]! =- 1 ) Continue  ; Que [rear ++] = V;  If (Rear = maxn) rear = 0  ; Dep [v] = Dep [u] + 1  ; ++ Gap [Dep [v] ;}} Int SAP ( Int Start, Int  End ){  Int Res = 0  ; BFS (START, end );  Int  Cur [maxn];  Int  S [maxn];  Int Top = 0  ; Memcpy (cur, Head,  Sizeof (Head ));  Int U = Start;  Int  I;  While (DEP [start] < N ){  If (U = End ){  Int Temp = INF;  Int  Inser;  For (I = 0 ; I <top; I ++ )  If (Temp> Edge [s [I]. Cap) {temp = Edge [s [I]. CAP; inser = I ;}  For (I = 0 ; I <top; I ++ ) {Edge [s [I]. Cap -= Temp; edge [s [I] ^ 1 ]. Cap + = Temp;} res + = Temp; top = Inser; u = Edge [s [Top]. From  ;}  If (U! = End & gap [Dep [u]- 1 ] = 0 ) //  Fault occurred, no zengguang Road            Break  ;  For (I = cur [u]; I! =- 1 ; I =Edge [I]. Next)  If (Edge [I]. Cap! = 0 & Amp; Dep [u] = Dep [edge [I]. To] + 1  )  Break  ;  If (I! =- 1  ) {Cur [u] = I; s [Top ++] = I; u = Edge [I]. ;} Else  {  Int Min = N;  For (I = head [u]; I! =- 1 ; I = Edge [I]. Next ){  If (Edge [I]. Cap = 0 ) Continue  ;  If (Min> Dep [edge [I]. To]) {min =Dep [edge [I]. To]; cur [u] = I ;}} -- Gap [Dep [u]; Dep [u] = Min + 1  ; ++ Gap [Dep [u];  If (U! = Start) u = edge [s [-- top]. From  ;}}  Return  Res ;}  Int  Main (){ //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int  Start, end;  Int  M;  Int  U, V, Z;  Int  T; scanf (  "  % D  " ,& T ); While (T -- ) {Init (); scanf (  "  % D  " , & N ,& M );  Int Minx = 10000000  ;  Int Maxx =- 10000000  ;  Int  X, Y;  For (Int I = 1 ; I <= N; I ++ ) {Scanf (  "  % D  " , & X ,& Y );  If (Minx> X) {minx = X; Start = I ;}  If (Maxx < X) {Maxx = X; End = I ;}}  While (M -- ) {Scanf (  "  % D  " , & U, & V ,& Z); addedge (u, v, Z); addedge (v, U, Z );}  //  N must be the total number of points, which must be noted when using the sap template.          Int Ans = SAP (START, end); printf (  " % D \ n  "  , ANS );}  Return   0  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.