1. 程式人生 > >(hdu-4280)Island Transport~測試網絡流模板速度~要加掛才能過啊

(hdu-4280)Island Transport~測試網絡流模板速度~要加掛才能過啊

south please mon ron follow incr dep 還要 att

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 這題就是測試模板的速度,我的模板還是不行啊,太慢了,還要加掛才能過 #pragma comment(linker, "/STACK:1024000000,1024000000")
收獲一個黑科技,不過也是卡著時間過的 9843 好慢啊。 差點就T了
  1 #include<stdio.h>
  2 #include<string>
  3 #include<string.h>
  4 #include<vector>
  5 #include<queue>
  6 using namespace std;
  7 #pragma comment(linker, "/STACK:1024000000,1024000000")
  8 const int maxn = 1e5 + 10;
  9 const int INF = 999999999;
 10 struct node {
 11     int from, to, cap, flow;
 12 };
 13 struct Dinic {
 14     int n, m, s, t;
 15     vector<node>nodes;
 16     vector<int>g[maxn];
 17     int vis[maxn];
 18     int d[maxn];
 19     int cur[maxn];
 20     void clearall(int n) {
 21         for (int i = 0 ; i < n ; i++) g[i].clear();
 22         nodes.clear();
 23     }
 24     void clearflow() {
 25         int len = nodes.size();
 26         for (int i = 0 ; i < len ; i++) nodes[i].flow = 0;
 27     }
 28     void add(int from, int to, int cap) {
 29         nodes.push_back((node) {
 30             from, to, cap, 0
 31         });
 32         nodes.push_back((node) {
 33             to, from, cap, 0
 34         });
 35         m = nodes.size();
 36         g[from].push_back(m - 2);
 37         g[to].push_back(m - 1);
 38     }
 39     bool bfs() {
 40         memset(vis, 0, sizeof(vis));
 41         queue<int>q;
 42         q.push(s);
 43         d[s] = 0;
 44         vis[s] = 1;
 45         while(!q.empty()) {
 46             int x = q.front();
 47             q.pop();
 48             int len = g[x].size();
 49             for (int i = 0 ; i < len ; i++) {
 50                 node &e = nodes[g[x][i]];
 51                 if (!vis[e.to] && e.cap > e.flow ) {
 52                     vis[e.to] = 1;
 53                     d[e.to] = d[x] + 1;
 54                     q.push(e.to);
 55                 }
 56             }
 57         }
 58         return vis[t];
 59     }
 60     int dfs(int x, int a) {
 61         if  (x == t || a == 0) return a;
 62         int flow = 0, f, len = g[x].size();
 63         for (int &i = cur[x] ; i < len ; i++) {
 64             node & e = nodes[g[x][i]];
 65             if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0 ) {
 66                 e.flow += f;
 67                 nodes[g[x][i] ^ 1].flow -= f;
 68                 flow += f;
 69                 a -= f;
 70                 if (a == 0) break;
 71             }
 72         }
 73         return flow;
 74     }
 75     int maxflow(int a, int b) {
 76         s = a;
 77         t = b;
 78         int flow = 0;
 79         while(bfs()) {
 80             memset(cur, 0, sizeof(cur));
 81             flow += dfs(s, INF);
 82         }
 83         return flow;
 84     }
 85     vector<int>mincut() {
 86         vector<int>ans;
 87         int len = nodes.size();
 88         for (int i = 0 ; i < len ; i++) {
 89             node & e = nodes[i];
 90             if ( vis[e.from] && !vis[e.to] && e.cap > 0 ) ans.push_back(i);
 91         }
 92         return ans;
 93     }
 94     void reduce() {
 95         int len = nodes.size();
 96         for (int i = 0 ; i < len ; i++) nodes[i].cap -= nodes[i].flow;
 97     }
 98 } f;
 99 int main() {
100     int n, m, t;
101     scanf("%d", &t);
102     while(t--) {
103         scanf("%d%d", &n, &m);
104         f.clearall(n);
105         f.clearflow();
106         int left = INF, right = -INF, s = 1, t = 1;
107         for (int i = 1 ; i <= n ; i++) {
108             int x, y;
109             scanf("%d%d", &x, &y);
110             if (x < left) {
111                 left = x;
112                 s = i;
113             }
114             if (x > right) {
115                 right = x;
116                 t = i;
117             }
118         }
119         for (int i = 0 ; i < m ; i++) {
120             int u, v, c;
121             scanf("%d%d%d", &u, &v, &c);
122             f.add(u, v, c);
123         }
124         printf("%d\n", f.maxflow(s, t));
125     }
126     return 0;
127 }

(hdu-4280)Island Transport~測試網絡流模板速度~要加掛才能過啊