HDU 4280 Island Transport
Island Transport
Time Limit: 10000ms
Memory Limit: 65536KB
This problem will be judged on HDU. Original ID: 428064-bit integer IO format: %I64d Java class name: Main
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.
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.
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.
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
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
9 6
Source
解题:网络流模板题,注意双向边,其实可以这样子搞~
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF = ~0U>>2; 4 const int maxn = 100010; 5 struct arc{ 6 int to,flow,next; 7 arc(int x = 0,int y = 0,int z = -1){ 8 to = x; 9 flow = y; 10 next = z; 11 } 12 }e[2000100]; 13 int head[maxn],d[maxn],gap[maxn],tot,S,T,n,m; 14 void add(int u,int v,int flow){ 15 e[tot] = arc(v,flow,head[u]); 16 head[u] = tot++; 17 e[tot] = arc(u,flow,head[v]); 18 head[v] = tot++; 19 } 20 queue<int>q; 21 void bfs(){ 22 for(int i = 0; i <= n; ++i){ 23 d[i] = -1; 24 gap[i] = 0; 25 } 26 d[T] = 0; 27 q.push(T); 28 while(!q.empty()){ 29 int u = q.front(); 30 q.pop(); 31 ++gap[d[u]]; 32 for(int i = head[u]; ~i; i = e[i].next){ 33 if(d[e[i].to] == -1){ 34 d[e[i].to] = d[u] + 1; 35 q.push(e[i].to); 36 } 37 } 38 } 39 } 40 int dfs(int u,int low){ 41 if(u == T) return low; 42 int tmp = 0,minH = n - 1; 43 for(int i = head[u]; ~i; i = e[i].next){ 44 if(e[i].flow){ 45 if(d[e[i].to] + 1 == d[u]){ 46 int a = dfs(e[i].to,min(low,e[i].flow)); 47 e[i].flow -= a; 48 e[i^1].flow += a; 49 low -= a; 50 tmp += a; 51 if(!low) break; 52 if(d[S] >= n) return tmp; 53 } 54 if(e[i].flow) minH = min(minH,d[e[i].to]); 55 } 56 } 57 if(!tmp){ 58 if(--gap[d[u]] == 0) d[S] = n; 59 ++gap[d[u] = minH + 1]; 60 } 61 return tmp; 62 } 63 int main(){ 64 int kase,x,y,s,t,u,v,w; 65 scanf("%d",&kase); 66 while(kase--){ 67 scanf("%d%d",&n,&m); 68 memset(head,-1,sizeof head); 69 s = INF; 70 int ret = t = 0; 71 for(int i = 1; i <= n; ++i){ 72 scanf("%d%d",&x,&y); 73 if(s > x){ 74 s = x; 75 S = i; 76 } 77 if(t < x){ 78 t = x; 79 T = i; 80 } 81 } 82 for(int i = tot = 0; i < m; ++i){ 83 scanf("%d%d%d",&u,&v,&w); 84 add(u,v,w); 85 } 86 bfs(); 87 while(d[S] < n) ret += dfs(S,INF); 88 printf("%d\n",ret); 89 } 90 return 0; 91 }
夜空中最亮的星,照亮我前行