Island Transport
Time limit:20000/10000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 10716 Accepted Submission (s): 3430
Problem Description in the vast waters far far away, there is many islands. People is living on the islands, and all of the transport among the islands relies on the ships.
You had a transportation company there. Some routes is opened for passengers. Each route was a straight line connecting the different islands, and it is bidirectional. Within an hour, a-route can transport a certain number of passengers in one direction. For safety, no routes is cross or overlap and no routes would pass an island except the departing island and the Arriv ING Island. Each of the island can is treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from
The transport capacity is important. Suppose many passengers depart from the "Westernmost island" and "would like" arrive at the easternmost island, the maximum Number of passengers arrive at the latter within every hour are 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 all test case contains, integers N and M (2<=n,m<=100000), the number of islands and the Numbe R of Routes. Islands is number from 1 to N.
Then N lines follow. Each line contain the integers, the X and Y coordinate of a island. The k-th line in the N lines describes the island K. The absolute values of all the coordinates is 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-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 a westernmost and only one island is easternmost. No. Islands would has the same coordinates. Each of the island can go to all other island by the routes.
Output for each test case, output a integer in one line, the transport capacity.
Sample Input2
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
5 | |
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 4Sample Output96Analysis: Compare bare maximum flow, first find starting point (minimum x coordinate) and end point (maximum x coordinate),using an array to simulate the edges of the linked list record, run the dinic to figure out the maximum flow. Submit 8330MS with g++, submit time Limit exceeded in C + + ...
#include <cstdio>#include<cstring>#include<algorithm>#include<queue>#defineINF 1000000007using namespacestd;intN,m,cnt,t,s;//T is the end pointstructnode{intT,c;} edge[500000];inthead[100010],next1[500000];intdis[100010];voidAddintSintTintc) {edge[cnt].t=T; EDGE[CNT].C=C; NEXT1[CNT]=Head[s]; Head[s]=cnt++;}intBFs () {memset (DIS,-1,sizeof(DIS)); Dis[s]=0; Queue<int>Q; Q.push (S); while(!Q.empty ()) { intu=Q.front (); Q.pop (); intk=Head[u]; while(k!=-1) { intv=edge[k].t; if(dis[v]==-1&&edge[k].c>0) {Dis[v]=dis[u]+1; Q.push (v); } k=Next1[k]; } } if(dis[t]>0)return 1; return 0;}intDfsintCurintm) { if(cur==t)returnm; intres=0, f,k=Head[cur]; while(k!=-1) { if(dis[edge[k].t]==dis[cur]+1&&edge[k].c>0&& (f=Dfs (Edge[k].t,min (M,EDGE[K].C)))) {EDGE[K].C-=F; Edge[k^1].c+=F; Res+=F; M-=F; if(!m) Break; } k=Next1[k]; } if(!res) dis[cur]=-2; returnRes;}intMain () {intCAs; scanf ("%d",&CAs); while(cas--) {memset (head,-1,sizeof(head)); scanf ("%d%d",&n,&M); intSx=inf,tx=-inf;//x coordinate of start s and end point Tcnt=s=t=0; for(intI=1; i<=n;i++) { intx, y; scanf ("%d%d",&x,&y); if(sx>=x) {sx=x; s=i;} if(tx<=x) {tx=x; t=i;} } for(intI=0; i<m;i++) { intS,t,c; scanf ("%d%d%d",&s,&t,&c); Add (S,T,C); Add (T,S,C); } intans=0, Res; while(BFS ()) while(Res=dfs (S,inf)) ans+=Res; printf ("%d\n", ans); } return 0;}View Code
hdu4280 Island Transport (maximum flow dinic array to simulate adjacent edges)