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 toN − 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 108. 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 108. It is guaranteed that Vovan's system can transport some positive quantity of oil, and can't transport more than 2·109 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):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = MAXN * ;
const int INF = 0x7fffffff; inline void _min(int &a, const int &b) {
if(a > b) a = b;
} struct SAP {
int head[MAXN], gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int next[MAXE], to[MAXE], cap[MAXE], flow[MAXE];
int ecnt, n, st, ed; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge2(int u, int v, int c) {
to[ecnt] = v; cap[ecnt] = c; flow[ecnt] = ; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cap[ecnt] = c; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
} void bfs() {
memset(dis, 0x3f, sizeof(dis));
queue<int> que; que.push(ed);
dis[ed] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
++gap[dis[u]];
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(cap[p ^ ] && dis[v] > n) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int Max_flow(int ss, int tt, int nn) {
st = ss, ed = tt, n = nn;
int ans = , minFlow = INF, u;
for(int i = ; i <= n; ++i) {
cur[i] = head[i];
gap[i] = ;
}
u = pre[st] = st;
bfs();
while(dis[st] < n) {
bool flag = false;
for(int &p = cur[u]; p; p = next[p]) {
int &v = to[p];
if(cap[p] > flow[p] && dis[u] == dis[v] + ) {
flag = true;
_min(minFlow, cap[p] - flow[p]);
pre[v] = u;
u = v;
if(u == ed) {
ans += minFlow;
while(u != st) {
u = pre[u];
flow[cur[u]] += minFlow;
flow[cur[u] ^ ] -= minFlow;
}
minFlow = INF;
}
break;
}
}
if(flag) continue;
int minDis = n - ;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(cap[p] > flow[p] && dis[v] < minDis) {
minDis = dis[v];
cur[u] = p;
}
}
if(--gap[dis[u]] == ) break;
++gap[dis[u] = minDis + ];
u = pre[u];
}
return ans;
}
} G; int x, y, n, m, a, b, c;
int id[MAXE]; int main() {
while(scanf("%d", &n) != EOF) {
for(int i = ; i <= n; ++i) scanf("%d%d", &x, &y);
G.init();
scanf("%d", &m);
for(int i = ; i <= m; ++i) {
scanf("%d%d%d", &a, &b, &c);
id[i] = G.ecnt;
G.add_edge2(a, b, c);
}
printf("%d\n", G.Max_flow(, n, n));
for(int i = ; i <= m; ++i) {
int &p = id[i];
if(G.flow[p] >= ) printf("%d %d %d\n", G.to[p ^ ], G.to[p], G.flow[p]);
else printf("%d %d %d\n", G.to[p], G.to[p ^ ], G.flow[p ^ ]);
}
}
}

URAL 1664 Pipeline Transportation(平面图最大流)的更多相关文章

  1. BZOJ 1001 [BeiJing2006] 狼抓兔子(平面图最大流)

    题目大意 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的.而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...

  2. 【 UVALive - 5095】Transportation(费用流)

    Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...

  3. s - t 平面图最大流 (附例题 bzoj 1001)

    以下均移自 周冬的<两极相通-浅析最大最小定理在信息学竞赛中的应用> 平面图性质 1.(欧拉公式)如果一个连通的平面图有n个点,m条边和f个面,那么f=m-n+2 2.每个平面图G都有一个 ...

  4. CodeForces E. Goods transportation【最大流+dp最小割】

    妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...

  5. 刷题向》图论》BZOJ1001 平面图最大流、平面图最小割、单源最短路(easy+)

    坦白的说这是一道水题,但是因为是BZOJ上的1001,所以这道题有着特殊的意义. 关于最大流转最短路的博客链接如下:关于最大流转最短路两三事 这道题的图形很规矩,所以建边和建点还是很简单的. 题目如下 ...

  6. BZOJ-1001 狼抓兔子 (最小割-最大流)平面图转对偶图+SPFA

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Submit: 14686 Solved: 3513 [Submit][ ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

  9. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

随机推荐

  1. 【Django笔记四】Django2.0中的表单

    一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53   安装mysql 二.基础信息 1.App中的模型mod ...

  2. 『ACM C++』 PTA 天梯赛练习集L1 | 050-51

    加油加油,努力刷题 ------------------------------------------------L1-050------------------------------------ ...

  3. SQL语句中生成UUID方法

    SQL语句中生成UUID方法为UUID() 生成带横线UUID: select UUID()                         形如:abaffaca-fd55-11e5-b3d0-d2 ...

  4. jquery的JSON字符串处理、单引号双引号的转换

    1.jquery的JSON字符串处理 var pwdlevel_val = "{"minLength":1,"maxLength":20," ...

  5. JS 原型总结

    参考: (从内存角度)简单类型与复杂类型及原型链

  6. vue的细节

    1.如果使用路由跳转到别的界面的话,例如从文章list页面跳转到具体文章查看详情页,查看某一个具体就需要传递那个文章的id去后台查询, this.$router.push的params方法可以实现传递 ...

  7. 【Hadoop故障处理】高可用(HA)环境DataNode问题

    [故障背景] NameNode和DataNode进程正常运行,但是网页找不到DataNode,DataNode为空.各个节点机器之间可以ping同主机名. [日志排查] 众多日志中找到如下关键点错误信 ...

  8. python -- 简单配置发送邮件功能

    本文用第三方类库:yagmail 实现:以QQ邮箱作为发送邮箱为例.最终的实现效果:给指定邮箱,发送指定内容的邮件. 准备工作 1.用于发送邮件的账号信息 比如账号用自己的qq邮箱,但'密码'需要在邮 ...

  9. Zookeeper -- 本地\完全分布式 搭建

    准备工作 linux软件:Zookeeper-3.4.12.tar.gz 四台centos系统虚拟机,主机名为:s101~s104 一.本地模式搭建(s101上安装) 1.解压软件压缩包:解压到根目录 ...

  10. PHP array_reduce()函数的应用解析

    实例 向用户自定义函数发送数组中的值,并返回一个字符串: <?php function myfunction($v1,$v2) { return $v1 . "-" . $v ...