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. 实现一个shell程序

    实现一个自己的shell程序,这个程序有这些功能:解释执行命令,支持输入输出重定向,支持管道,后台运行 程序.当运行该程序后,它支持以下的命令格式: 1.单个命令,如:ls.2.带l到多个参数的命令, ...

  2. 09JavaScript函数

    函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 实例1: <!DOCTYPE html> <html> <head> <meta charset ...

  3. ACM 2000~2002

    ACM  2000  输入三个字符后,按各个字符的ASCⅡ码从小打到的顺序输出这三个字符. import java.util.Scanner; public class Lengxc {public ...

  4. bootstrap-paginator分页插件的简单使用实例

    Document 21:36:40 简述:bootstrap-paginator是一款基于bootstrap的jQuery分页插件. githup项目地址:https://github.com/lyo ...

  5. Centos7 Redis3.0 集群搭建备忘

    (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下) 127.0.0.1:7000 127.0.0. ...

  6. 『Python基础-15』递归函数 Recursion Function

    什么是递归函数 一种计算过程,如果其中每一步都要用到前一步或前几步的结果,称为递归的.用递归过程定义的函数,称为递归函数,例如连加.连乘及阶乘等.凡是递归的函数,都是可计算的,即能行的. 递归就是一个 ...

  7. fiddler响应报文的headers属性详解

    fiddler响应报文的headers属性详解 (1)Cache头域 1. Cache-Control 在请求报文已经说过了,用于设置缓存的属性,浏览内容不被缓存. 2. Data 生成消息的具体时间 ...

  8. js分片上传大文件,前端代码

    首先导入jQuery.form.js文件,下面src是相对于改js文件位置, <script type="text/JavaScript" src="jquery/ ...

  9. C#中如何使用JS脚本

    C#中如何使用JS脚本 目前在做的组态软件中就使用到了js脚本,这部分js脚本是供用户编写的,用户可以通过我们提供的脚本以及js自身的逻辑,用户就可以随心所欲的控制设备的运行.有比较了几款在C#中执行 ...

  10. java入门---对象和类&概念详解&实例

        Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载     这篇文章,我们主要来看下: 对象:对象是类的一个实例(对象不是找个女朋友),有状态 ...