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 ...
随机推荐
- MySQL Innodb表空间不足的处理方法
官方给出的解决方案: 添加和删除 InnoDB 数据和日志文件 这一节描述在InnoDB表空间耗尽空间之时,或者你想要改变日志文件大小之时,你可以做的一些事情. 最简单的,增加InnoDB表空间大小的 ...
- 作业题:闰年 if((year%4==0&&year%100!=0)||year&400==0)
作业题:闰年 if((year%4==0&&year%100!=0)||year&400==0)
- 关于flyme5显示不到和卸载不到旧应用解决方法
笔者买入一台mx5,升级flyme5后旧应用没有显示出来,而且在设置的应用管理都没显示旧应用. 通过adb命令: adb shell pm list packages显示所有包名, 查看自己要删除应用 ...
- Git基本操作笔记:初始化,用户设置,撤销修改
1. Git 初始化 git init git remote add repos_name repos_url git add . git commit -m 'commit message' gi ...
- JAVA 修改环境变量不重启电脑生效方法
1. 在安装JDK1.6(高版本)时(本机先安装jdk1.6再安装的jdk1.5),自动将java.exe.javaw.exe.javaws.exe三个可执行文件复制到了C:\Windows\Sys ...
- sphinx中文入门指南 (转自sphinx中文站)
Sphinx中文入门指南 wuhuiming<blvming在gmail.com>,转载请注明来源和作者 最后修改:2010年1月23日 1.简介 1.1.Sphinx是什么 1.2.Sp ...
- Vue实例和生命周期
创建一个Vue实例 每个Vue应用都是通过Vue函数创建一个新的Vue实例开始: var vm = new Vue({ //选项 }) 数据与方法 当一个Vue实例被创建时,它向Vue的响应式系统中加 ...
- thinkcmf常用标签
1.图片地址:{:cmf_get_image_url($vo.icon)} 2.模板控件 模板变量调用:$theme_vars.title <widget name="aboutUs& ...
- python3 循环输出当前时间。
题目 暂停一秒输出(使用 time 模块的 sleep() 函数).循环输出当前时间. 代码: import time while True: time.sleep(1) print(time.str ...
- python3 完全平方数(循环)
题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 代码: for i in range(1,85): if 168 % i == 0: j = 168 ...