URAL 1664 Pipeline Transportation(平面图最大流)

Description

An oligarch Vovan, as many other oligarchs, transports oil from West Cuckooland to East Cuckooland. He owns a huge oil-producing station in West Cuckooland, an equally huge oil-refining station in East Cuckooland and a system of oil pipelines to move oil from one country to another. Vovan has a map of these pipelines on his table. He would like to know, how much oil this system can transport.
Each pipeline connects some pair of stations. All stations on the map are numbered: the producing station has number 1, the refining one has number   N  and the transit ones have numbers from 2 to N − 1, inclusive. Each pipeline can transport a limited quantity of oil, but in any direction. Vovan doesn't know that the Earth is round, so each station on his map has plane coordinates ( xi  and   yi  are the coordinates of   i-th station). The pipelines are represented as line segments. Any pair of pipelines on the map can intersect only at endpoints. It is known, that the oil-producing station has the smallest   x-coordinate of all stations, and the oil-refining station has the largest   x-coordinate.

Input

The first line contains an integer   N. 2 ≤  N ≤ 10000. Next   N  lines contain the coordinates of the stations ( xiyi) separated with a space. Coordinates are integers with absolute values no more than 10 8. Next line contains an integer   M — the number of oil pipelines. Next   M  lines contain specifications of pipelines: for each pipeline, the three numbers describe a pair of stations connected by it and its flow capacity — an integer from 1 to 10 8. It is guaranteed that Vovan's system can transport some positive quantity of oil, and can't transport more than 2·10 9  oil units.

Output

In the first line output the maximal quantity of oil that the Vovan's system can transport. In the following   M  lines output the transportation plan — triples of numbers ( ABC), denoting that   C  oil units should flow from station   A  to station   B. All pipelines should be presented exactly once in this list (even those, in which the oil flow is equal to zero). The values of   C  should always be non-negative.

 

题目大意:从源点1到汇点n有m条双向边,每条边有一个容量,问从1到n的最大流量,并输出每条边的流量(如果是0要输出0,是从流入点到流出点)

思路:直接从1到n求最大流过了……本意是要转平面图最短路径的?

PS:根据那个不知道什么定理,好像边数最多是2*N-3

 

代码(437MS):

  1 #include <cstdio>

  2 #include <cstring>

  3 #include <iostream>

  4 #include <algorithm>

  5 #include <queue>

  6 using namespace std;

  7 

  8 const int MAXN = 10010;

  9 const int MAXE = MAXN * 10;

 10 const int INF = 0x7fffffff;

 11 

 12 inline void _min(int &a, const int &b) {

 13     if(a > b) a = b;

 14 }

 15 

 16 struct SAP {

 17     int head[MAXN], gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];

 18     int next[MAXE], to[MAXE], cap[MAXE], flow[MAXE];

 19     int ecnt, n, st, ed;

 20 

 21     void init() {

 22         memset(head, 0, sizeof(head));

 23         ecnt = 2;

 24     }

 25 

 26     void add_edge2(int u, int v, int c) {

 27         to[ecnt] = v; cap[ecnt] = c; flow[ecnt] = 0; next[ecnt] = head[u]; head[u] = ecnt++;

 28         to[ecnt] = u; cap[ecnt] = c; flow[ecnt] = 0; next[ecnt] = head[v]; head[v] = ecnt++;

 29     }

 30 

 31     void bfs() {

 32         memset(dis, 0x3f, sizeof(dis));

 33         queue<int> que; que.push(ed);

 34         dis[ed] = 0;

 35         while(!que.empty()) {

 36             int u = que.front(); que.pop();

 37             ++gap[dis[u]];

 38             for(int p = head[u]; p; p = next[p]) {

 39                 int &v = to[p];

 40                 if(cap[p ^ 1] && dis[v] > n) {

 41                     dis[v] = dis[u] + 1;

 42                     que.push(v);

 43                 }

 44             }

 45         }

 46     }

 47 

 48     int Max_flow(int ss, int tt, int nn) {

 49         st = ss, ed = tt, n = nn;

 50         int ans = 0, minFlow = INF, u;

 51         for(int i = 0; i <= n; ++i) {

 52             cur[i] = head[i];

 53             gap[i] = 0;

 54         }

 55         u = pre[st] = st;

 56         bfs();

 57         while(dis[st] < n) {

 58             bool flag = false;

 59             for(int &p = cur[u]; p; p = next[p]) {

 60                 int &v = to[p];

 61                 if(cap[p] > flow[p] && dis[u] == dis[v] + 1) {

 62                     flag = true;

 63                     _min(minFlow, cap[p] - flow[p]);

 64                     pre[v] = u;

 65                     u = v;

 66                     if(u == ed) {

 67                         ans += minFlow;

 68                         while(u != st) {

 69                             u = pre[u];

 70                             flow[cur[u]] += minFlow;

 71                             flow[cur[u] ^ 1] -= minFlow;

 72                         }

 73                         minFlow = INF;

 74                     }

 75                     break;

 76                 }

 77             }

 78             if(flag) continue;

 79             int minDis = n - 1;

 80             for(int p = head[u]; p; p = next[p]) {

 81                 int &v = to[p];

 82                 if(cap[p] > flow[p] && dis[v] < minDis) {

 83                     minDis = dis[v];

 84                     cur[u] = p;

 85                 }

 86             }

 87             if(--gap[dis[u]] == 0) break;

 88             ++gap[dis[u] = minDis + 1];

 89             u = pre[u];

 90         }

 91         return ans;

 92     }

 93 } G;

 94 

 95 int x, y, n, m, a, b, c;

 96 int id[MAXE];

 97 

 98 int main() {

 99     while(scanf("%d", &n) != EOF) {

100         for(int i = 1; i <= n; ++i) scanf("%d%d", &x, &y);

101         G.init();

102         scanf("%d", &m);

103         for(int i = 1; i <= m; ++i) {

104             scanf("%d%d%d", &a, &b, &c);

105             id[i] = G.ecnt;

106             G.add_edge2(a, b, c);

107         }

108         printf("%d\n", G.Max_flow(1, n, n));

109         for(int i = 1; i <= m; ++i) {

110             int &p = id[i];

111             if(G.flow[p] >= 0) printf("%d %d %d\n", G.to[p ^ 1], G.to[p], G.flow[p]);

112             else printf("%d %d %d\n", G.to[p], G.to[p ^ 1], G.flow[p ^ 1]);

113         }

114     }

115 }

 

你可能感兴趣的:(pipeline)