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 【费用流】的更多相关文章

  1. POJ3068 "Shortest" pair of paths

    嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...

  2. 2018.06.27"Shortest" pair of paths(费用流)

    "Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...

  3. poj 3068 "Shortest" pair of paths

    "Shortest" pair of paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1407 ...

  4. "Shortest" pair of paths[题解]

    "Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...

  5. POJ 3068 "Shortest" pair of paths(费用流)

    [题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...

  6. UVALIVE 2927 "Shortest" pair of paths

    裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...

  7. UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解

    题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...

  8. [poj] 3068 "Shortest" pair of paths || 最小费用最大流

    [原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...

  9. POJ3068:"Shortest" pair of paths——题解

    http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...

随机推荐

  1. JavaScript高级程序设计-读书笔记(6)

    第20章 JSON JSON是一个轻量级的数据格式,可以简化表示复杂数据结构的工作量 JSON的语法可以表示一下三种类型的值 l        简单值:使用与JavaScript相同的语法,可以在JS ...

  2. shell脚本中case select 的使用

    #!/bin/bash # case echo "1.Install PHP" echo "2.Install Mysql" echo "3.Inst ...

  3. springboot项目属性配置及注意事项

    在idea编辑器建的springboot项目中的resources包下的application.properties这个就是配置文件. 另外配置文件的文件名还可以是application.yml,在r ...

  4. [spring]<context:property-placeholder/>

    问题: 有些参数在某些阶段中是常量,这些参数在不同阶段之间又往往需要改变,如: 在开发阶段我们连接数据库时的url,username,password等信息 分布式应用中client端的server地 ...

  5. 使用jQuery插件jRemoteValidate进行远程ajax验证,可以自定义返回的信息

    最近项目中有一个业务是收银员通过输入用户卡号,给用户充值或者消费,但是为了避免误操作(如卡号输错),于是编写了一个远程验证的jQuery插件, 当收银员输入卡号后,失去焦点,立即ajax请求服务器端, ...

  6. settings.xml配置文件详解

    简单值 一半顶层settings元素是简单值,它们表示的一系列值可以配置Maven的核心行为:settings.xml中的简单顶层元素 < settings xmlns="http:/ ...

  7. 4: 模块化应用程序开发 Modular Application Development Using Prism Library 5.0 for WPF (英汉对照版)

    A modular application is an application that is divided into a set of loosely coupled functional uni ...

  8. C# 中移动文件到指定位置

    根据文件后缀名称将文件移动到指定的文件夹下面,具体代码如下: demo中使用的是 .png 具体的情况根据你的需求可以更改 using System; using System.IO; public ...

  9. C# 解决串口接收数据不完整

    方法1: 使 用缓存机制完成.首先通过定义一个成员变量List<byte> buffer = new List<byte> (4096);用来存放所有的数据,在接收函数里,通过 ...

  10. windows配置redis(转)

    此文章全部是转的,我之前是woidows启动redis无法加载配置找到的:原文链接:http://www.cnblogs.com/smileyearn/articles/4749746.html 在w ...