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

“Shortest” pair of paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1589 Accepted: 708
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
Source

Southeastern Europe 2006

题意简述:给你一个网络,让你找两条从000号点到n−1n-1n1号点的两条不走相同边的最短路,走重边算走不同的路。显然是一道费用流,我们将除000号点和n−1n-1n1号点以外的每个点拆点,并连一条容量为111的边来限制经过的路径条数,然后再建立源点,汇点分别与000号点和n−1n-1n1号点连容量为222的边来保证最多走两条路,这样跑费用流,若跑出来最大流&lt;2&lt;2<2,说明没有两条满足条件的路径,输出NotpossibleNot possibleNotpossible,否则输出最小费用即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#define N 1000
#define M 100000
#define inf 0x3f3f3f3f
using namespace std;
inline long long read(){
	long long ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
	return ans;
}
struct Node{int v,next,c,w;}e[M];
int pos[N],d[N],flow[N],first[N],pred[N],n,m,s,t,cnt,tot=0;
bool in[N];
inline void add(int u,int v,int c,int w){
	e[++cnt].v=v;
	e[cnt].w=w;
	e[cnt].c=c;
	e[cnt].next=first[u];
	first[u]=cnt;
	e[++cnt].v=u;
	e[cnt].w=-w;
	e[cnt].c=0;
	e[cnt].next=first[v];
	first[v]=cnt;
}
inline bool spfa(){
	queue<int>q;
	memset(flow,inf,sizeof(flow));
	memset(d,inf,sizeof(d));
	memset(in,false,sizeof(in));
	q.push(s),in[s]=true,d[s]=0,pred[t]=-1;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		in[x]=false;
		for(int i=first[x];i!=-1;i=e[i].next){
			int v=e[i].v;
			if(e[i].c>0&&d[v]>d[x]+e[i].w){
				d[v]=d[x]+e[i].w,pred[v]=x,pos[v]=i,flow[v]=min(flow[x],e[i].c);
				if(!in[v])in[v]=true,q.push(v);
			}
		}
	}
	return pred[t]!=-1;
}
inline void solve(){
	int maxf=0,maxw=0;
	while(spfa()){
		maxf+=flow[t],maxw+=flow[t]*d[t];
		int now=t;
		while(now!=s){
			e[pos[now]].c-=flow[t];
			e[pos[now]^1].c+=flow[t];
			now=pred[now];
		}
	}
	printf("Instance #%d: ",++tot);
	if(maxf<2)printf("Not possible\n");
	else printf("%d\n",maxw);
}
int main(){
	while(1){
		n=read(),m=read(),s=0,t=(n<<1|1);
		if(!n&&!m)break;
		memset(e,0,sizeof(e));
		memset(first,-1,sizeof(first));
		cnt=-1;
		add(s,1,2,0),add(n+n,t,2,0),add(1,1+n,2,0),add(n,n+n,2,0);
		for(int i=2;i<n;++i)add(i,i+n,1,0);
		for(int i=1;i<=m;++i){
			int u=read()+1,v=read()+1,w=read();
			add(u+n,v,1,w);
		}
		solve();
	}
	return 0;
}

转载于:https://www.cnblogs.com/ldxcaicai/p/9738566.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值