POJ3068 "Shortest" pair of paths 【费用流】
POJ3068 “Shortest” pair of paths
Description
A chemical company has an unusual shortest path problem.
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
The input will consist of multiple cases. The first line of each input will contain N and M where N is the number of depots and M is the number of individual shipping methods. You may assume that N is less than 64 and that M is less than 10000. The next M lines will contain three values, i, j, and v. Each line corresponds a single, unique shipping method. The values i and j are the indices of two depots, and v is the cost of getting from i to j. Note that these shipping methods are directed. If something can be shipped from i to j with cost 10, that says nothing about shipping from j to i. Also, there may be more than one way to ship between any pair of depots, and that may be important here.
A line containing two zeroes signals the end of data and should not be processed.
Output
follow the output format of sample 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
题意是有两个物品,需要把他们从0号节点运送到n-1号节点,其中他们经过的路径除了0和n-1两个节点是完全不相同的,每个边有边权,求最小化边权,不能满足输出Not possible
然我我们发现每个点是有度数限制的,所以我们将一个点拆成两个点,中间连接一条cap为1的边,但是对于s和t(0和n-1),中间连接的边是度数为2,跑完最大流之后检查一下是不是流量为二,是二就存在可行流,否则不存在
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f
struct Edge{
int u,v,cap,flow,cost;
Edge(int xu,int xv,int xcap,int xflow,int xcost){
u=xu;v=xv;cap=xcap;flow=xflow;cost=xcost;
}
};
struct MCMF{
int s,t;
int d[N],f[N],p[N];
bool inq[N];
vector<Edge> E;
vector<int> G[N];
void clear(){
E.clear();
for(int i=0;i<N;i++)G[i].clear();
}
void add(int u,int v,int cap,int cost){
E.push_back(Edge(u,v,cap,0,cost));
E.push_back(Edge(v,u,0,0,-cost));
int m=E.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
bool SPFA(int &flow,int &cost){
memset(inq,0,sizeof(inq));
memset(d,0x3f,sizeof(d));
queue<int> Q;Q.push(s);
d[s]=0;f[s]=INF;
while(!Q.empty()){
int u=Q.front();Q.pop();
inq[u]=0;
for(int i=0;i<G[u].size();i++){
Edge e=E[G[u][i]];
if(d[e.v]>d[u]+e.cost&&e.cap>e.flow){
d[e.v]=d[u]+e.cost;
f[e.v]=min(f[u],e.cap-e.flow);
p[e.v]=G[u][i];
if(!inq[e.v]){
inq[e.v]=1;
Q.push(e.v);
}
}
}
}
if(d[t]==INF)return false;
flow+=f[t];cost+=d[t]*f[t];
int u=t;
while(u!=s){
E[p[u]].flow+=f[t];
E[p[u]^1].flow-=f[t];
u=E[p[u]].u;
}
return true;
}
int Min_cost_Max_flow(){
int flow=0,cost=0;
while(SPFA(flow,cost));
return (flow==2)?cost:-1;
}
}mcmf;
int n,m,ind=0;
int main(){
while(scanf("%d%d",&n,&m)&&n&&m){
mcmf.clear();
mcmf.s=1;mcmf.t=n*2;
mcmf.add(1,2,2,0);
for(int i=1;i<n;i++)mcmf.add(i*2-1,i*2,1,0);
mcmf.add(n*2-1,n*2,2,0);
for(int i=1;i<=m;i++){
int u,v,c;scanf("%d%d%d",&u,&v,&c);
u++;v++;
mcmf.add(u*2,v*2-1,1,c);
}
int cost=mcmf.Min_cost_Max_flow();
if(cost==-1)printf("Instance #%d: Not possible\n",++ind);
else printf("Instance #%d: %d\n",++ind,cost);
}
return 0;
}
POJ3068 "Shortest" pair of paths 【费用流】的更多相关文章
- POJ3068 "Shortest" pair of paths
嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- poj 3068 "Shortest" pair of paths
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1407 ...
- "Shortest" pair of paths[题解]
"Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...
- POJ 3068 "Shortest" pair of paths(费用流)
[题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...
- UVALIVE 2927 "Shortest" pair of paths
裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...
- UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解
题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...
- [poj] 3068 "Shortest" pair of paths || 最小费用最大流
[原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...
- POJ3068:"Shortest" pair of paths——题解
http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...
随机推荐
- git bash 出显错误不能用,怎么解决
解决方法: 好像就是64的会出问题,其实32位的git也可以安装在64位的系统上. 将你64位的git卸掉了后,下载一个32位的git安装,就可以正常使用了, 当然,你的32位的出了错,卸了后也这样处 ...
- HTML符号大全
HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全. ...
- android之代码混淆
项目发布之前混淆是必不可少的工作,混淆可以增加别人反编译阅读代码的难度,还可以缩小APK包. Android 中通过ProGuard 来混淆Java代码,仅仅是混淆java代码.它是无法混淆Nativ ...
- RocketMQ学习分享
消息队列的流派 什么是 MQ Message Queue(MQ),消息队列中间件.很多人都说:MQ 通过将消息的发送和接收分离来实现应用程序的异步和解偶,这个给人的直觉是——MQ 是异步的,用来解耦的 ...
- 联表更新SQL语句
联表更新语句第一次写,,,主要是在实现功能上需要向repay_detail添加一个新的字段item_id.但是以前的老数据的话这个字段的值就为null 所以就写了下面一条语句就更新了老数据...SQL ...
- (1) iOS开发之UI处理-预览篇
不管是做iOS还是Android的开发,我想UI这块都是个大麻烦,任何客户端编程都是如此,我们要做的就是尽量减少我们工作的复杂度,这样才能更轻松的工作. 在iOS开发中Xcode虽然自带了强大的IB( ...
- Android面试二之Fragment
基本概念 Fragment,简称碎片,是Android 3.0(API 11)提出的,为了兼容低版本,support-v4库中也开发了一套Fragment API,最低兼容Android 1.6. F ...
- python类常用装饰器
class Myclass(object): def __init__(self): pass #必须实例化才能调用 def sayhi(self): print 'hello' #静态方法,跟类没什 ...
- taskset -pc PID 查看线程占用cpu核
taskset -pc PID 可以用于 查看 当前线程 对应绑定的 在 哪个核上面. 这个 可以用于 程序优化, 查看 哪个线程占用的 cpu 比重比较高 首先 可以通过 top -H - ...
- JavaScript数字和字符串转换示例
http://www.jb51.net/article/48465.htm 1. 数字转换为字符串 a. 要把一个数字转换为字符串,只要给它添加一个空的字符串即可: 复制代码代码如下: var n = ...