POJ3068 "Shortest" pair of paths
来源:互联网 发布:jsp引入java 编辑:程序博客网 时间:2024/06/01 09:23
"Shortest" pair of paths
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1385 Accepted: 609
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
Source
Southeastern Europe 2006
———————————————————————————————————
题意:N个仓库由M条有向边连接,每条边都有一定费用。将两种危险品从0运到N-1,除了起点和终点外,危险品不能放在一起,也不能走相同的路径。求最小费用?
思路:除了起点和终点,不能放一起,说明普通顶点的容量都是1,起点终点容量为2。对所有边,追加容量=1;对起点,增加一个源点,建一条容量为2,费用为0的边,连到起点,对终点,增加一个汇点,从终点建一条容量为2,费用0的边到汇点。然后炮最小费用最大流
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;#define MAXN 60100#define MAXM 1000100int vis[MAXN],d[MAXN],pre[MAXN],a[MAXN];struct Edge{ int u, v, c, cost, next;} edge[MAXM];int s[MAXN], cnt;void init(){ cnt = 0; memset(s, -1, sizeof(s));}void add(int u, int v, int c, int cost){ edge[cnt].u = u; edge[cnt].v = v; edge[cnt].cost = cost; edge[cnt].c = c; edge[cnt].next = s[u]; s[u] = cnt++; edge[cnt].u = v; edge[cnt].v = u; edge[cnt].cost = -cost; edge[cnt].c = 0; edge[cnt].next = s[v]; s[v] = cnt++;}bool spfa(int ss, int ee,int &flow,int &cost){ queue<int> q; memset(d, INF, sizeof d); memset(vis, 0, sizeof vis); d[ss] = 0, vis[ss] = 1, pre[ss] = 0, a[ss] = INF; q.push(ss); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for (int i = s[u]; ~i; i = edge[i].next) { int v = edge[i].v; if (edge[i].c>0&& d[v]>d[u] + edge[i].cost) { d[v] = d[u] + edge[i].cost; pre[v] = i; a[v] = min(a[u], edge[i].c); if (!vis[v]) { vis[v] = 1; q.push(v); } } } } if (d[ee] == INF) return 0; flow += a[ee]; cost += d[ee]*a[ee]; int u = ee; while (u != ss) { edge[pre[u]].c -= a[ee]; edge[pre[u] ^ 1].c += a[ee]; u = edge[pre[u]].u; } return 1;}int MCMF(int ss, int ee){ int cost = 0, flow=0; while (spfa(ss, ee, flow, cost)); if(flow<2) return -1; return cost;}int main(){ int m,n,u,v,c; int cas=1; while(~scanf("%d%d",&n,&m)&&(m||n)) { init(); for(int i=0; i<m; i++) { scanf("%d%d%d",&u,&v,&c); add(u,v,1,c); } add(n,0,2,0); add(n-1,n+1,2,0); printf("Instance #%d: ",cas++); int ans=MCMF(n,n+1); if(ans!=-1) printf("%d\n",ans); else printf("Not possible\n"); } return 0;}
阅读全文
0 0
- POJ3068 "Shortest" pair of paths
- poj3068 "Shortest" pair of paths 费用流
- POJ3068: "Shortest" pair of paths 题解
- POJ3068 "Shortest" pair of paths最小费用流
- POJ3068--"Shortest" pair of paths(最小费用流)
- POJ-3068-"Shortest" pair of paths
- poj 3068 "Shortest" pair of paths
- "Shortest" pair of paths - POJ 3068 费用流
- POJ 3068 "Shortest" pair of paths 已翻译
- poj 3068 "Shortest" pair of paths(最小费用流)
- POJ 3068 "Shortest" pair of paths(最小费用最大流-mcmf)
- Poj 3068 "Shortest" pair of paths【拆点+最小费用最大流】
- poj3068
- (Shortest Paths Algorithm)
- Double Shortest Paths CSU
- ABC 051D Candidates of No Shortest Paths 最短路(Floyd)
- Shortest paths and matrix multiplication
- uva 12821 Double Shortest Paths
- 工厂方法模式
- C++ 单生产者多消费者多线程示例
- tcp超时重传
- UML序列图——时序图基本使用
- Codeforces Round #386 (Div. 2) C
- POJ3068 "Shortest" pair of paths
- Maven详解之仓库------本地仓库和远程仓库
- ListView上拉刷新 下拉加载
- Eclipse 运行WordCount实例 (连接Linux下的Hadoop集群)
- git学习心得之2--糟糕操作后的后悔药
- 高通 MSM8K bootloader 之一: SBL1
- Docker在Windows下的安装使用与常见问题
- 自定义评分条(方式一)-CustomRatingBar
- python 批量安装模块