hdu 1385 Minimum Transport Cost 最短路加路径输出

时间:2021-08-01 15:54:12


Problem DescriptionThese 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.
 
InputFirst 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:
 
OutputFrom 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
50 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-->3Total cost : 21From 3 to 5 :Path: 3-->4-->5Total cost : 16From 2 to 4 :Path: 2-->1-->5-->4Total cost : 17

题目大意:

有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度。

如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费)。

现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和。

求最小花费,如果有多条路经符合,则输出字典序最小的路径。

刚开始用dijkstra每次调用,一直超时,后来发现还要字典序最小路径,用dijkstra超时改不了,字典序又不会弄,换方法吧,后改成floyd还是超时,hdu 1385 Minimum Transport Cost 最短路加路径输出改了N久,最终路径正着存,直接输出,AC
#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;int n;int Edge[110][110];int e[110][110];int path[110][110];int a[110];void floyd(){    for(int i=1; i<=n; i++)        for(int j=1; j<=n; j++)        {            e[i][j]=Edge[i][j];            if(i!=j&&e[i][j]<INF)                path[i][j]=j;            //path[i][j]=i;            else                path[i][j]=-1;        }    for(int k=1; k<=n; k++)        for(int i=1; i<=n; i++)            for(int j=1; j<=n; j++)            {                if(k==i||k==j)                    continue;                if(e[i][k]+e[k][j]+a[k]<e[i][j])                {                    e[i][j]=e[i][k]+e[k][j]+a[k];  //最短路径                    path[i][j]=path[i][k];                }                else if(e[i][k]+e[k][j]+a[k]==e[i][j])                {                    path[i][j]=min(path[i][j],path[i][k]); //字典序最小                    //path[i][j]=min(path[i][j],path[k][j]);                }            }}int main(){    while(~scanf("%d",&n))    {        if(n==0)            break;        for(int i=1; i<=n; i++)            for(int j=1; j<=n; j++)            {                scanf("%d",&Edge[i][j]);                if(Edge[i][j]==-1)                    Edge[i][j]=INF;            }        for(int i=1; i<=n; i++)            scanf("%d",&a[i]);        floyd();        int s,t;        while(~scanf("%d%d",&s,&t))        {            if(s==-1&&t==-1)                break;            printf("From %d to %d :\n",s,t);            printf("Path: ");            int k=s;            printf("%d",s);            while(k!=t)            {                printf("-->%d",path[k][t]);  //直接输出路径                k=path[k][t];            }            printf("\n");//           int shortest[110];//            memset(shortest,0,sizeof(shortest));//            int k=0;//            shortest[k]=t;//            while(path[s][shortest[k]]!=s)  //先将路径保存到shortest数组再输出路径//            {//                k++;//                shortest[k]=path[s][shortest[k-1]];//            }//            k++;//            shortest[k]=s;//            printf("Path: ");//            for(int l=k; l>0; l--)//                printf("%d-->",shortest[l]);//            printf("%d\n",shortest[0]);            printf("Total cost : %d\n\n",e[s][t]);        }    }    return 0;}