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. RSA非对称加密算法

    基本定义: RSA公开密钥密码体制.所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制.在公开密钥密码体制中,加密密钥(即公开密钥 ...

  2. angular2.x 下拉多选框选择组件

    angular2.x - 5.x 的下拉多选框选择组件 ng2 -- ng5.最近在学angular4,经常在交流群看见很多人问 下拉多选怎么做... 今天就随便写的个. 组件源码 百度云   链接: ...

  3. kibana 安装

    一 介绍 kibana 主要实现对日志的可视化显示. 二 安装 下载安装包: wget https://download.elastic.co/kibana/kibana/kibana-4.1.2-l ...

  4. Spring Cloud实战

    Spring Cloud实战(一)-Spring Cloud Config Server https://segmentfault.com/a/1190000006149891 https://seg ...

  5. 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...

  6. Wannafly挑战赛14E无效位置

    https://www.nowcoder.com/acm/contest/81/E 给一个1-base数组{a},有N次操作,每次操作会使一个位置无效.一个区间的权值定义为这个区间里选出一些数的异或和 ...

  7. Verilog HDL Test Bench

    As digital systems becomes more complex,it becomes increasingly important to verify the functionalit ...

  8. 使用samba初始化开发环境

    一.系统环境 [root@host-172-20-3-66 samba]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 搞 ...

  9. Find the Longest Word in a String

    找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. 这是一些对你有帮助的资源: String.split() String.length 第一种想法就是,先定一个小变量,来他一 ...

  10. vue组件间传值

    父传子 1.父组件:在子组件容器内绑定数据 <router-view :unusedOrderNum="num1" :usedOrderNum="num2" ...