"Shortest" pair of paths - POJ 3068 费用流
来源:互联网 发布:淘宝产品视频制作价格 编辑:程序博客网 时间:2024/06/06 00:19
"Shortest" pair of paths
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1066 Accepted: 421
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.
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.
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 10 1 202 30 1 200 1 201 0 104 60 1 221 3 110 2 142 3 260 3 430 3 580 0
Sample Output
Instance #1: Not possibleInstance #2: 40Instance #3: 73
题意:让你找出从0到n-1的两条路线,使得不重复经过某一个点,并且要求路径的总长度最短。
思路:用费用流,拆点,使得除了起始点外的点都只经过一次,跑出流量为2,即可。
AC代码如下:
#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node{ int v,cost,cap,next;}edge[30010];int t,n,m,Head[170],tot,F,F2,C,dis[170],INF=1e9,pre[170],f[170];bool vis[170];queue<int> qu;void add(int u,int v,int cost,int cap){ edge[tot].v=v; edge[tot].cost=cost; edge[tot].cap=cap; edge[tot].next=Head[u]; Head[u]=tot++;}int _spfa(){ int i,j,k,u,v,p; for(i=1;i<=n;i++) dis[i]=INF; memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); memset(f,0,sizeof(f)); dis[1]=0; vis[1]=1; qu.push(1); f[1]=F2-F; pre[1]=-1; while(!qu.empty()) { u=qu.front(); qu.pop(); vis[u]=0; for(p=Head[u];p!=-1;p=edge[p].next) { v=edge[p].v; if(edge[p].cap && dis[v]>dis[u]+edge[p].cost) { dis[v]=dis[u]+edge[p].cost; f[v]=min(f[u],edge[p].cap); pre[v]=p; if(!vis[v]) { vis[v]=1; qu.push(v); } } } } if(dis[n]==INF) return 0; C+=f[n]*dis[n]; F+=f[n]; if(F==F2) return 0; for(p=pre[n];p!=-1;p=pre[edge[p^1].v]) { edge[p].cap-=f[n]; edge[p^1].cap+=f[n]; } return 1;}int main(){ int i,j,k,u,v,cost,cap; while(~scanf("%d%d",&n,&m) && n>0) { memset(Head,-1,sizeof(Head)); tot=0; n--; for(i=1;i<n;i++) { add(i*2,i*2^1,0,1); add(i*2^1,i*2,0,0); } for(i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&cost); if(u==n || v==0) continue; add(u*2^1,v*2,cost,1); add(v*2,u*2^1,-cost,0); } n*=2; F2=2; C=F=0; while(_spfa()) { //printf("F=%d C=%d\n",F,C); } if(F!=2) printf("Instance #%d: Not possible\n",++t); else printf("Instance #%d: %d\n",++t,C); }}
0 0
- "Shortest" pair of paths - POJ 3068 费用流
- poj 3068 "Shortest" pair of paths(最小费用流)
- poj3068 "Shortest" pair of paths 费用流
- POJ 3068 "Shortest" pair of paths(最小费用最大流-mcmf)
- Poj 3068 "Shortest" pair of paths【拆点+最小费用最大流】
- POJ-3068-"Shortest" pair of paths
- poj 3068 "Shortest" pair of paths
- POJ3068 "Shortest" pair of paths最小费用流
- POJ3068--"Shortest" pair of paths(最小费用流)
- POJ 3068 "Shortest" pair of paths 已翻译
- POJ3068 "Shortest" pair of paths
- POJ3068: "Shortest" pair of paths 题解
- 【费用流】CSU 1506 Double Shortest Paths
- 费用流 csu1506 Double Shortest Paths
- CSU 1506 - Double Shortest Paths(网络流’最小费用流)
- CSU 1506: Double Shortest Paths(最小费用最大流)
- CSU 1506 Double Shortest Paths(最小费用流)
- Double Shortest Paths(费用流)2014年省赛D题
- extern "C"的用法解析
- Java虚拟机学习摘要
- 手把手教你使用start uml画类图
- 为什么这样能够防止头文件重复调用?
- Linux内核分析(三)内核启动过程分析——构造一个简单的Linux系统
- "Shortest" pair of paths - POJ 3068 费用流
- Android进程与线程基本知识
- 数据结构学习第二课2
- HTML第九节(css样式基本知识)
- php优化技巧
- 每个Java程序员必须知道的5个JVM命令行标志
- UIView 中与动画相关的各函数 详介
- nginx 正则表达式 匹配
- JSP-servlet 网址