poj 3068 "Shortest" pair of paths
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1407 | Accepted: 627 |
Description
There are N depots (vertices) where chemicals can be stored. There are M individual shipping methods (edges) connecting pairs of depots. Each individual shipping method has a cost. In the usual problem, the company would need to find a way to route a single shipment from the first depot (0) to the last (N - 1). That's easy. The problem they have seems harder. They have to ship two chemicals from the first depot (0) to the last (N - 1). The chemicals are dangerous and cannot safely be placed together. The regulations say the company cannot use the same shipping method for both chemicals. Further, the company cannot place the two chemicals in same depot (for any length of time) without special storage handling --- available only at the first and last depots. To begin, they need to know if it's possible to ship both chemicals under these constraints. Next, they need to find the least cost of shipping both chemicals from first depot to the last depot. In brief, they need two completely separate paths (from the first depot to the last) where the overall cost of both is minimal.
Your program must simply determine the minimum cost or, if it's not possible, conclusively state that the shipment cannot be made.
Input
A line containing two zeroes signals the end of data and should not be processed.
Output
Sample Input
2 1
0 1 20
2 3
0 1 20
0 1 20
1 0 10
4 6
0 1 22
1 3 11
0 2 14
2 3 26
0 3 43
0 3 58
0 0
Sample Output
Instance #1: Not possible
Instance #2: 40
Instance #3: 73 题意:从入口到出口,同时运送两批化学药品,且规定两批药品运送过程中都不能经过任意一条一样的路(即一批药经过的路另一批药运送不能经过),且使得运送两批药品的总路程尽量小,应该是多少。
思路:建立源点S,S到路口连费用0,流量2的边,建立汇点t,终点到t也建立流量为2费用0的边,中间的图的流量都为1,建图,求最小费用流即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<functional>
using namespace std;
#define INF 0x3f3f3f3f
const int N_MAX = ,V_MAX=; typedef pair<int, int>P;
struct edge { int to, cap, cost, rev;
edge(int to=,int cap=,int cost=,int rev=):to(to),cap(cap),cost(cost),rev(rev) {}
};
int V;
vector<edge>G[V_MAX];
int h[V_MAX];
int dist[V_MAX];
int prevv[V_MAX], preve[V_MAX]; void add_edge(int from,int to,int cap,int cost) {
G[from].push_back(edge(to,cap,cost,G[to].size()));
G[to].push_back(edge(from,,-cost,G[from].size()-));
}
//没流量则返回-1
int min_cost_flow(int s,int t,int f) {
int res = ;
fill(h, h + V, );
while (f>) {
priority_queue<P, vector<P>, greater<P> >que;
fill(dist,dist+V,INF);
dist[s] = ;
que.push(P(,s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (dist[v] < p.first)continue;
for (int i = ; i < G[v].size();i++) {
edge&e = G[v][i];
if (e.cap > && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to],e.to));
}
}
}
if (dist[t] == INF)return -;
for (int v = ; v < V; v++)h[v] += dist[v];
int d = f;
for (int v = t; v != s;v=prevv[v]) {
d = min(d,G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for (int v = t; v != s;v=prevv[v]) {
edge&e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
} void clear() {
for (int i = ; i < V;i++) {
G[i].clear();
}
} int N, M; int main() {
int cs=;
while (scanf("%d%d",&N,&M)&&N) {
int s = N, t = s + ;
V = t+;
for (int i = ; i < M;i++) {
int u, v, cost;
scanf("%d%d%d",&u,&v,&cost);
add_edge(u,v,,cost);
}
add_edge(s, , , );
add_edge(N-,t,,);
int res = min_cost_flow(s, t, );
printf("Instance #%d: ", ++cs);
if(res!=-)printf("%d\n",res);
else printf("Not possible\n");
clear();
}
return ;
}
poj 3068 "Shortest" pair of paths的更多相关文章
- POJ 3068 "Shortest" pair of paths(费用流)
[题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...
- [poj] 3068 "Shortest" pair of paths || 最小费用最大流
[原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- POJ3068 "Shortest" pair of paths 【费用流】
POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...
- "Shortest" pair of paths[题解]
"Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...
- POJ3068:"Shortest" pair of paths——题解
http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...
- UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解
题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...
- POJ3068 "Shortest" pair of paths
嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...
- UVALIVE 2927 "Shortest" pair of paths
裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...
随机推荐
- PAT (Basic Level) Practise (中文)-1019. 数字黑洞 (20)
http://www.patest.cn/contests/pat-b-practise/1019 给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第 ...
- Tomcat详细安装配置
1.首先是Tomcat的获取和安装. 获取当然得上Apache的官方网站下载,开源免费,而且带宽也足够.下载会很快. 这是两种不同的下载,一个是普通安装版本,一个是解压安装版本.使用起来是一样的,只是 ...
- IOS使用Jenkins进行持续集成
本文主要讲述在开发过程中,提高工作效率而进行的IOS-Jenkins的持续集成. 背景 平时我们开发完成IOS项目,需要打包给测试人员进行测试.其中的过程需要重复进行:修改配置项--编译---连接设备 ...
- 20181111 计时器影响DOM点击事件的逻辑
今天在群里看见一个人在问"点击按钮使图片产生旋转为什么要使用计时器来实现",我自己操作了一遍她的代码才发现里面的逻辑实现很有意思,所以写出来分享一下. 她的代码是这样写的: < ...
- 你对CommonJS规范了解多少?
写在前面 为什么会出现CommonJS规范? 因为JavaScript本身并没有模块的概念,不支持封闭的作用域和依赖管理,传统的文件引入方式又会污染变量,甚至文件引入的先后顺序都会影响整个项目的运行. ...
- Android驱动开发读书笔记五
第五章 本章介绍了S3C6410开发板的功能,开发板的不同主要是在烧录嵌入式系统的方式不同,以及如何在此开发板上安装Android. 1.安装串口调试工具minicom 首先需要一根USB转串口线,由 ...
- 救援(BFS)
题目描述: 在你的帮助下,Oliver终于追到小X了,可有一天,坏人把小X抓走了.这正是Oliver英雄救美的时候.所以,Oliver又找到哆啦A梦,借了一个机器,机器显示出一幅方格地图,它告诉Oli ...
- leetcode-15-basic-string
58. Length of Last Word 解题思路: 从结尾向前搜索,空格之前的就是最后一个词了.写的时候我考虑了尾部有空格的情况.需要注意的是,测试用例中有" "的情况,此 ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- poj-3009 curling2.0(搜索)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26408 Accepted: 10546 Des ...