Discussion
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点,花费为0,容量为2,n-1点到汇点,花费为0,容量为2.
其他路的容量都是1.
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <string>
#include <map>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
const int N = 220;
int n,m;
struct Node
{
int from,to,cap,flow,cost;
};
vector<Node> e;
vector<int> v[N];
int vis[N],dis[N]; // 标记 源点到各点的最小花费
int p[N],a[N]; // 记录路径
void Clear(int x)
{
for(int i=0;i<=x;i++)
v[i].clear();
e.clear();
}
void add_Node(int from,int to,int cap,int cost)
{
e.push_back((Node){from,to,cap,0,cost});
e.push_back((Node){to,from,0,0,-cost});
int len = e.size()-1;
v[to].push_back(len); //按照输入的顺序记录关系
v[from].push_back(len-1);
}
bool SPFA(int s,int t,int& flow,int& cost)
{
Del(dis,inf); Del(vis,0);
dis[s] = 0; vis[s] = 1;
p[s] = 0; a[s] = inf;
queue<int> q;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = 0;
for(unsigned short i=0; i<v[u].size(); i++)
{
Node& g = e[v[u][i]];
if(g.cap>g.flow && dis[g.to] > dis[u]+g.cost)
{
dis[g.to] = dis[u] + g.cost;
p[g.to] = v[u][i];
a[g.to] = min(a[u],g.cap-g.flow);//路径上的最小流量
if(!vis[g.to])
{
q.push(g.to);
vis[g.to]=1;
}
}
}
}
if(dis[t] == inf)
return false;
flow += a[t];
cost += dis[t]*a[t];
int u = t;
while(u!=s)
{
e[p[u]].flow += a[t];
e[p[u]^1].flow -= a[t];
u = e[p[u]].from;
}
return true;
}
int Min_Cost(int s,int t,int& flow)
{
int cost = 0;
while(SPFA(s,t,flow,cost));
return cost;
}
int main()
{
int k = 0;
while( scanf("%d %d",&n,&m) != EOF && n|m)
{
k++;
int flow = 0;
add_Node(0,1,2,0); //添加源点
add_Node(n,n+1,2,0);//添加汇点
for(int i = 0; i < m; i ++)
{
int u,v,cost;
scanf("%d %d %d",&u,&v,&cost);
add_Node(u+1,v+1,1,cost);
}
int x = Min_Cost(0,n+1,flow);
if(x && flow == 2 ) printf("Instance #%d: %d\n",k,x);
else printf("Instance #%d: Not possible\n",k);
Clear(n+1);
}
}