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. 在Java中用正则表达式判断一个字符串是否是数字的方法

    package chengyujia; import java.util.regex.Pattern; public class NumberUtil { /** * 判断一个字符串是否是数字. * ...

  2. java中反射的基本使用

    fanShe.java package example5;class fanShe{    /*1.应用在一些通用性比较高的代码中.     *2.后面学的框架,大多数都是应用框架来实现的.     ...

  3. windows10下“sqlplus / as sysdba”执行提示无权限解决办法

    ORA_DBA:是ORACLE 的特有用户,是超级管理员权限,建成DBA 它具有管理数据库的最高权限. 注明:需要以管理员身份运行cmd,不然第4步会失败(点开始,输入cmd,右键以管理员身份运行) ...

  4. memcache类的扩展函数

    Memcache — Memcache类 /****连接****/1.Memcache::connect – 创建一个Memcache对象语法:bool Memcache::connect ( str ...

  5. 【NXP开发板应用—智能插排】4. PWM驱动

    [前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...

  6. inotify和epoll

    参考EventHub.cpp 1.初始化inotify int mINotifyFd = inotify_init(); 2.将要监测的目录添加到inotify int result = inotif ...

  7. python学习笔记:第18天 面向对象04-反射

    目录 issubclass和isinstance 区分函数和方法 反射 issubclass和isinstance issubclass:可以判断一个类是否另一个类的子类. # issubclass ...

  8. 决策树&随机森林

    参考链接: https://www.bilibili.com/video/av26086646/?p=8 <统计学习方法> 一.决策树算法: 1.训练阶段(决策树学习),也就是说:怎么样构 ...

  9. DMVPN的实验模拟与分析

    此篇博客正在介绍的是下图中的DMVPN: 为什么会出现DMVPN这个技术呢? 在这篇博客中https://www.cnblogs.com/huwentao/p/9355240.html介绍过Dynam ...

  10. centos7 杂记

    yum 源 https://www.cnblogs.com/renpingsheng/p/7845096.html 安装nginx php mysql https://www.cnblogs.com/ ...