HDU 4280 最大流Dinic算法优化

Island Transport

 

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
这题BFS中用queue会超时,而优化就是用数组模拟queue操作。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 #define INF 0x3f3f3f3f
 6 using namespace std;
 7 const int MAX = 100010;
 8 int T, d[MAX], start, tend;
 9 struct node {
10     int to, cap, next;
11 }edge[MAX<<1];
12 int head[MAX];
13 bool vis[MAX];
14 int cnt;
15 void add_edge(int start, int to, int cap) {
16     edge[cnt].to = to;
17     edge[cnt].cap = cap;
18     edge[cnt].next = head[start];
19     head[start] = cnt++;
20 }
21 bool bfs() {
22     memset(d, -1, sizeof(d));
23     int Q[MAX<<1];
24     int Thead = 0, Ttail = 0;
25     Q[Ttail++] = start;
26     d[start] = 0;
27     while(Thead < Ttail) {
28         int x = Q[Thead];
29         if(x == tend) return true;
30         for(int i = head[x]; i != -1; i = edge[i].next) {
31             int tmp = edge[i].to;
32             if(d[tmp] == -1 && edge[i].cap > 0) {
33                 d[tmp] = d[x] + 1;
34                 Q[Ttail++] = tmp;
35             }
36         }
37         Thead++;
38     }
39     return false;
40 }
41 int dfs(int x, int cap) {
42     if(x == tend) return cap;
43     int flow = 0, f;
44     for(int i = head[x]; i != -1; i = edge[i].next) {
45         int tmp = edge[i].to;
46         if(d[tmp] == d[x] + 1 && edge[i].cap) {
47             f = dfs(tmp,min(cap-flow,edge[i].cap));
48             edge[i].cap -= f;
49             edge[i^1].cap += f;
50             flow += f;
51             if(flow == cap)break;
52         }
53     }
54     if(!flow) d[x] = -2;
55     return flow;
56 }
57 int max_flow() {
58     int flow = 0, f;
59     while(bfs()) {
60         while((f = dfs(start,INF)) > 0) flow += f;
61     }
62     return flow;
63 }
64 int main() {
65     int n, m;
66     scanf("%d",&T);
67     while(T--) {
68         scanf("%d %d",&n,&m);
69         cnt = 0;
70         memset(head, -1, sizeof(head));
71         int x, y, u, v, w;
72         int tmax = -INF, tmin = INF;
73         start = tend = 1;
74         for(int i = 1; i <= n; i ++) {
75             scanf("%d %d",&x,&y);
76             if(x >= tmax) tmax = x, tend = i;
77             if(x <= tmin) tmin = x, start = i;
78         }
79         while(m--) {
80             scanf("%d%d%d",&u,&v,&w);
81             add_edge(u,v,w);
82             add_edge(v,u,w);
83         }
84         int ans = max_flow();
85         printf("%d\n",ans);
86     }
87     return 0;
88 }

 

posted @ 2017-08-01 20:15  starry_sky  阅读(324)  评论(0编辑  收藏  举报