Problem Description
These are N cities in Spring country. Between each pair of cities there may be one transportation track or none. Now there is some cargo that should be delivered from one city to another. The transportation fee consists of two parts:
The cost of the transportation on the path between these cities, and
a certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities.
You must write a program to find the route which has the minimum cost.
The cost of the transportation on the path between these cities, and
a certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities.
You must write a program to find the route which has the minimum cost.
Input
First is N, number of cities. N = 0 indicates the end of input.
The data of path cost, city tax, source and destination cities are given in the input, which is of the form:
a11 a12 ... a1N
a21 a22 ... a2N
...............
aN1 aN2 ... aNN
b1 b2 ... bN
c d
e f
...
g h
where aij is the transport cost from city i to city j, aij = -1 indicates there is no direct path between city i and city j. bi represents the tax of passing through city i. And the cargo is to be delivered from city c to city d, city e to city f, ..., and g = h = -1. You must output the sequence of cities passed by and the total cost which is of the form:
The data of path cost, city tax, source and destination cities are given in the input, which is of the form:
a11 a12 ... a1N
a21 a22 ... a2N
...............
aN1 aN2 ... aNN
b1 b2 ... bN
c d
e f
...
g h
where aij is the transport cost from city i to city j, aij = -1 indicates there is no direct path between city i and city j. bi represents the tax of passing through city i. And the cargo is to be delivered from city c to city d, city e to city f, ..., and g = h = -1. You must output the sequence of cities passed by and the total cost which is of the form:
Output
From c to d :
Path: c-->c1-->......-->ck-->d
Total cost : ......
......
From e to f :
Path: e-->e1-->..........-->ek-->f
Total cost : ......
Note: if there are more minimal paths, output the lexically smallest one. Print a blank line after each test case.
Path: c-->c1-->......-->ck-->d
Total cost : ......
......
From e to f :
Path: e-->e1-->..........-->ek-->f
Total cost : ......
Note: if there are more minimal paths, output the lexically smallest one. Print a blank line after each test case.
Sample Input
5 0 3 22 -1 4 3 0 5 -1 -1 22 5 0 9 20 -1 -1 9 0 4 4 -1 20 4 0 5 17 8 3 1 1 3 3 5 2 4 -1 -1 0
Sample Output
From 1 to 3 : Path: 1-->5-->4-->3 Total cost : 21 From 3 to 5 : Path: 3-->4-->5 Total cost : 16 From 2 to 4 : Path: 2-->1-->5-->4 Total cost : 17
题解:这道题我真的很无语啊。就是字典序让我浪费了好久啊。其实只需要在路径相等的时候,根据前驱判断那个字典序小。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int INF = 0x3fffffff;
int map[1003][1003];
int res[1003];
int c[1003];
int pre[1003];
bool visited[1003];
bool ok(int x,int y) //根据前驱找字典序
{
stack<int> s1;
stack<int> s2;
while(x != pre[x]) //
{
s1.push(x);
x = pre[x];
}
s1.push(x);
while(y != pre[y])
{
s2.push(y);
y = pre[y];
}
s2.push(y);
while(!s1.empty() && !s2.empty())
{
if(s1.top() < s2.top()) //按顺序比较,有一个小于就说明字典序要小
{
return true;
}
else if(s1.top() > s2.top()) //否则大
{
return false;
}
s1.pop(); //相等继续比较(这里只有起点相等)
s2.pop();
}
return true;
}
void prim(int x,int n)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++) //从起点开始更新数组
{
res[i] = map[x][i];
if(res[i] != -1 && i != x)
{
res[i] += c[i];
}
pre[i] = x; //前驱都指向起点,对于不相邻的也没关系,后面会更新
}
visited[x] = true;
for(int i = 1;i < n;i++) //找最短边
{
int min = INF;
int k;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && min > res[j] && res[j] != -1)
{
min = res[j];
k = j;
}
}
if(min == INF) //更新完毕
{
break;
}
visited[k] = true;
for(int j = 1;j <= n;j++)
{
//如果以前不相邻,现在可以到达的或者现在的小于以前的或者等于并且字典序小的,更新
if(!visited[j] && map[k][j] != -1 && (res[j] >= res[k] + map[k][j] + c[j] || -1 == res[j] ))
{
if(-1 == res[j] || res[j] > res[k] + map[k][j] + c[j])
{
res[j] = res[k] + map[k][j] + c[j];
pre[j] = k;
}
else if(res[j] == res[k] + map[k][j] + c[j] && ok(k,j)) //字典序小的
{
res[j] = res[k] + map[k][j] + c[j];
pre[j] = k;
}
}
}
}
}
void dfs(int x)
{
if(x == pre[x])
{
printf("%d",x);
}
else
{
dfs(pre[x]);
printf("-->%d",x);
}
}
int main()
{
int n;
int u,v;
while(scanf("%d",&n) && n != 0)
{
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
scanf("%d",&map[i][j]);
}
}
for(int i = 1;i <= n;i++)
{
scanf("%d",&c[i]);
}
int m = 0;
while(scanf("%d%d",&u,&v))
{
if(-1== u && -1 == v)
{
break;
}
prim(u,n);
printf("From %d to %d :\n",u,v);
if(u == v) //这里是看别人说的
{
printf("Path: %d",u);
printf("\nTotal cost : 0\n\n");
}
else
{
printf("Path: ");
dfs(v);
printf("\nTotal cost : %d\n\n",res[v] - c[v]);
}
}
}
return 0;
}