找起点到终点,两条点不重复权值之和最小的路径,拆点费即可可。注意判断满流
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.
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 1000
#define MAXM 100000
#define INF 0xFFFFFF
struct edge
{
int to,c,w,next;
};
edge e[MAXM];
bool in[MAXN];
int head[MAXN],dis[MAXN],pre[MAXN],en,maxflow,mincost;
int vn,st,ed;
int n,m;
void add(int a,int b,int c,int d)
{
e[en].to=b;
e[en].c=c;
e[en].w=d;
e[en].next=head[a];
head[a]=en++;
e[en].to=a;
e[en].c=0;
e[en].w=-d;
e[en].next=head[b];
head[b]=en++;
}
bool spfa(int s)
{
queue<int> q;
for(int i=0;i<=vn;i++)
{
dis[i]=INF;
in[i]=false;
pre[i]=-1;
}
dis[s]=0;
in[s]=true;
q.push(s);
while(!q.empty())
{
int tag=q.front();
in[tag]=false;
q.pop();
for(int i=head[tag];i!=-1;i=e[i].next)
{
int j=e[i].to;
if(e[i].w+dis[tag]<dis[j] && e[i].c)
{
dis[j]=e[i].w+dis[tag];
pre[j]=i;
if(!in[j])
{
q.push(j);
in[j]=true;
}
}
}
}
if(dis[ed]==INF)
return false;
return true;
}
void update()
{
int flow=INF;
for (int i=pre[ed];i!=-1;i=pre[e[i^1].to])
if(e[i].c<flow) flow=e[i].c;
for (int i=pre[ed];i!=-1;i=pre[e[i^1].to])
{
e[i].c-=flow,e[i^1].c+=flow;
}
maxflow+=flow;
mincost+=flow*dis[ed];
}
void mincostmaxflow()
{
maxflow=0,mincost=0;
while(spfa(st))
update();
}
int main()
{
for(int cs=1;~scanf("%d%d",&n,&m);cs++)
{
if(n==0 && m==0) break;
memset(head,-1,sizeof(head));en=0;
st=2*n,ed=2*n+1,vn=2*n+2;
for(int i=0;i<n;i++) add(i,i+n,1,0);
add(st,0+n,2,0);add(n-1,ed,2,0);
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u+n,v,1,w);
}
mincostmaxflow();
printf("Instance #%d: ",cs);
if(maxflow<2) printf("Not possible\n");
else printf("%d\n",mincost);
}
return 0;
}