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.
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:
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.
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
题意:给出一个地图,每个城市都有一个花费,城市与城市之间也有花费,给你多个询问,让你输出询问的城市到城市需要最少花费多少钱(起始城市和终点城市无花费),并输出路径,如果有多条路径,打印字典序最小的一条路径。
思路:按照SPFA跑一遍最短路,在松弛操作的时候,如果dis[y]==dis[now]+side[i].cost+val[y],那么就判断一下当前条路径与修改后的路径的字典序,如果修改后的字典序更小,那么就让使得字典序更小的那个点入队。
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1009;
const int inf=0x3f3f3f3f;
int head[maxn],book[maxn],dis[maxn],pre[maxn],val[maxn],cnt,n,pos;
struct node{
int id;
int cost;
int next;
}side[maxn*maxn];
void init()
{
memset(head,-1,sizeof(head));
memset(val,0,sizeof(val));
cnt=0;
}
void dfs(int now,char *ss)
{
if(now==-1) return ;
dfs(pre[now],ss);
ss[pos++]=now+'0';
}
int cmp(int before,int now)
{
char s1[1009],s2[1009];
pos=0;
dfs(before,s1);
s1[pos]='\0';
pos=0;
dfs(now,s2);
s2[pos++]=before+'0';
s2[pos]='\0';
if(strcmp(s1,s2)>0)
return 1;
else
return 0;
}
void add(int x,int y,int d)
{
side[cnt].id=y;
side[cnt].cost=d;
side[cnt].next=head[x];
head[x]=cnt++;
}
void SPFA(int sx)
{
queue<int>q;
dis[sx]=0;
book[sx]=1;
q.push(sx);
while(q.size())
{
int now=q.front();
q.pop();
book[now]=0;
for(int i=head[now];i!=-1;i=side[i].next)
{
int y=side[i].id;
if(dis[y]>dis[now]+side[i].cost+val[y])
{
dis[y]=dis[now]+side[i].cost+val[y];
pre[y]=now;
if(book[y]==0)
{
book[y]=1;
q.push(y);
}
}
else if(dis[y]==dis[now]+side[i].cost+val[y]&&cmp(y,now))
{
pre[y]=now;
if(book[y]==0)
{
book[y]=1;
q.push(y);
}
}
}
}
}
void print(int e)
{
printf("Path: ");
vector<int> st;
int now=e;
while(now!=-1)
{
st.push_back(now);
now=pre[now];
}
for(int i=st.size()-1;i>=1;i--)
printf("%d-->",st[i]);
printf("%d\n",st[0]);
}
int main()
{
int s,e;
while(~scanf("%d",&n)&&n)
{
init();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&s);
if(i==j||s==-1) continue;
add(i,j,s);
}
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
while(scanf("%d%d",&s,&e))
{
memset(book,0,sizeof(book));
memset(pre,-1,sizeof(pre));
memset(dis,inf,sizeof(dis));
if(s==-1&&e==-1) break;
if(s==e)
{
printf("From %d to %d :\n",s,e);
printf("Path: %d\n",s);
printf("Total cost : 0\n\n");
continue;
}
SPFA(s);
printf("From %d to %d :\n",s,e);
print(e);
printf("Total cost : %d\n\n",dis[e]-val[e]);
}
}
return 0;
}