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. Javascript混淆与解混淆的那些事儿

    像软件加密与解密一样,javascript的混淆与解混淆同属于同一个范畴.道高一尺,魔高一丈.没有永恒的黑,也没有永恒的白.一切都是资本市场驱动行为,现在都流行你能为人解决什么问题,这个概念.那么市场 ...

  2. Hadoop 动态扩容 增加节点

    基础准备 在基础准备部分,主要是设置hadoop运行的系统环境 修改系统hostname(通过hostname和/etc/sysconfig/network进行修改) 修改hosts文件,将集群所有节 ...

  3. JPMML解析PMML模型并导入数据进行分析生成结果

    JPMML解析Random Forest模型并使用其预测分析 导入Jar包 maven 的pom.xml文件中添加jpmml的依赖 <dependency> <groupId> ...

  4. 《TCP/IP详解 卷1:协议》第3章 IP:网际协议

    3.1 引言 IP是TCP/IP协议族中最为核心的协议.所有的TCP.UDP.ICMP及IGMP数据都以IP数据报格式传输(见图1-4).许多刚开始接触TCP/IP的人对IP提供不可靠.无连接的数据报 ...

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

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

  6. ACM1003:Max Sum

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

  7. Python中可变数据类型和不可变数据类型

    首先,我们需要知道在python中哪些是可变数据类型,哪些是不可变数据类型.可变数据类型:列表list和字典dict:不可变数据类型:整型int.浮点型float.字符串型string和元组tuple ...

  8. Java——多线程---18.11.22

    多线程代码:Runnable方法 package com.hebust.java.third; import java.util.Random; public class SaleTicket imp ...

  9. spark2.2 从入门到精通 视频教程 百度云网盘下载地址

    spark2.2 从入门到精通 视频教程 百度云网盘下载地址 链接:https://pan.baidu.com/s/1sm2Jdmt 密码:rdea

  10. PHP.52-TP框架商城应用实例-前台4-商品详情页-面包屑导航、AJAX浏览历史

    面包屑导航  思路:根据商品的主分类向上取出所有上级分类即可 1.在分类模型中增加取出所有上级分类的方法 /********** [面包屑导航]取出一个分类所有上级分类 **********/ pub ...