HDU4280(KB11-G 最大流)

Island Transport

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 9151    Accepted Submission(s): 2958


Problem Description

  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.
 

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

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 //2017-08-24
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <vector>
  8 #pragma comment(linker, "/STACK:1024000000,1024000000") 
  9 
 10 using namespace std;
 11 
 12 const int N = 100010;
 13 const int INF = 0x3f3f3f3f;
 14 int head[N], tot;
 15 struct Edge{
 16     int next, to, w;
 17 }edge[N<<4];
 18 
 19 void add_edge(int u, int v, int w){
 20     edge[tot].w = w;
 21     edge[tot].to = v;
 22     edge[tot].next = head[u];
 23     head[u] = tot++;
 24 }
 25 
 26 struct Dinic{
 27     int level[N], S, T;
 28     void init(int _S, int _T){
 29         S = _S;
 30         T = _T;
 31         tot = 0;
 32         memset(head, -1, sizeof(head));
 33     }
 34     bool bfs(){
 35         queue<int> que;
 36         memset(level, -1, sizeof(level));
 37         level[S] = 0;
 38         que.push(S);
 39         while(!que.empty()){
 40             int u = que.front();
 41             que.pop();
 42             for(int i = head[u]; i != -1; i = edge[i].next){
 43                 int v = edge[i].to;
 44                 int w = edge[i].w;
 45                 if(level[v] == -1 && w > 0){
 46                     level[v] = level[u]+1;
 47                     que.push(v);
 48                 }
 49             }
 50         }
 51         return level[T] != -1;
 52     }
 53     int dfs(int u, int flow){
 54         if(u == T)return flow;
 55         int ans = 0, fw;
 56         for(int i = head[u]; i != -1; i = edge[i].next){
 57             int v = edge[i].to, w = edge[i].w;
 58             if(!w || level[v] != level[u]+1)
 59                   continue;
 60             fw = dfs(v, min(flow-ans, w));
 61             ans += fw;
 62             edge[i].w -= fw;
 63             edge[i^1].w += fw;
 64             if(ans == flow)return ans;
 65         }
 66         if(ans == 0)level[u] = -2;
 67         return ans;
 68     }
 69     int maxflow(){
 70         int flow = 0, tmp;
 71         while(bfs())
 72               while((tmp = dfs(S, INF)) > 0)
 73                   flow += tmp;
 74         return flow;    
 75     }
 76 }dinic;
 77 
 78 int main()
 79 {
 80     //std::ios::sync_with_stdio(false);
 81     //freopen("inputG.txt", "r", stdin);
 82     int T, n, m;
 83     scanf("%d", &T);
 84     while(T--){
 85         scanf("%d%d", &n, &m);
 86         int x, y, s = 1, t = 1, mininum = INF, maxinum = -INF;
 87         for(int i = 1; i <= n; i++){
 88             scanf("%d%d", &x, &y);
 89             if(x < mininum){
 90                 mininum = x;
 91                 s = i;
 92             }
 93             if(x > maxinum){
 94                 maxinum = x;
 95                 t = i;
 96             }
 97         }
 98         dinic.init(s, t);
 99         int u, v, w;
100         for(int i = 0; i < m; i++){
101             scanf("%d%d%d", &u, &v, &w);
102             add_edge(u, v, w);
103             add_edge(v, u, w);
104         }
105         printf("%d
", dinic.maxflow());
106     }    
107     return 0;
108 }
原文地址:https://www.cnblogs.com/Penn000/p/7422303.html