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 ...
随机推荐
- hbuilder 夜神模拟器调试方法
1.首先下载好夜神模拟器2.查找已经安装的夜神模拟的端口,这里说一下夜神模拟器默认端口是62001,但是有些版本可能不是这个端口,怎么查找到底是哪个端口呢?按照如下顺序进行就可以查找到你按装的夜神模拟 ...
- vue-awesome-swiper插件爬坑
最近自己在做一个基于vue的知乎的移动端单页面,遇到很多坑,先说一下遇到最大的坑,其实并不推荐使用 vue-awesome-swiper,如果项目应用轮播,切换少的话.言归正传,现在来介绍vue-aw ...
- NOIP2013 乌龟棋
描述 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起点出发走到终点 ...
- php进行文件的强制下载
浏览器下载文件,例如在浏览器中可以直接打开的文件(.gif /.txt等).在进行文件下载操作时,默认是通过浏览器直接打开,而不是下载保存文件.并且通过这种方法下载文件可以不暴漏下载文件所在的路径,可 ...
- Python pip 使用国内镜像
## 推荐源```https://mirrors.aliyun.com/pypi/simple/ 阿里镜像,速度快.稳定https://pypi.douban.com/simple/ 豆瓣镜像```# ...
- python入门:从安装python开始
python简介: Python (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明 ...
- python数据类型之集合(set)和其常用方法
集合是一个无序的,不重复的数据组合 作用(集合的重点):1.去重,把一个列表变成集合就自动去重了2.关系测试,测试两组数据库之前的交集.差集.并集等关系 s = {1, 1, 2, 2, 3, 4, ...
- 基于IAR6或者IAR7建立STM32开发工程(通过实际测试,使用IAR6.30.4)
IAR和keil两个开发平台都是arm开发当中比较流行的平台,keil4的版本之间,可以兼容,但是版本4和版本5还是不兼容的,但是IAR的兼容性更加差,好像6.30.x之间是能够兼容的吧,没有实测过, ...
- light oj 1104 Birthday Paradox (概率题)
Sometimes some mathematical results are hard to believe. One of the common problems is the birthday ...
- 谋哥:搞APP,做得累的都不对!
最近谋哥(微信viyi88)我刚加入“秦王会”,思想收到猛烈地冲击,各位大佬的思维有时候会让我大脑短路,收获不少.同时,我也慢慢发现我一直平静的 心开始浮躁,我发现苗头不对,于是开始静下心来.静下心, ...