Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 7007 Accepted Submission (s): 1791
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 shoshould be delivered from one city to another. the transportation extends 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, Please t 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 43 0 5 -1 -122 5 0 9 20-1 -1 9 0 44 -1 20 4 05 17 8 3 11 33 52 4-1 -10
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
SourceAsia 1996, Shanghai (Mainland China)
The most basic floyd is the shortest path .....
There are two path recording methods:
Exit: fa [a] [B] = a, fa [a] [B] = c indicates a... c-> B
Exit: For fa [a] [B] = B, fa [a] [B] = c indicates a-> c... B
Select the second option based on the question
#include
#include
#include
#include
#include using namespace std;const int INF=0x3f3f3f3f;int n;int g[110][110],b[110];int fa[110][110];int main(){ while(scanf("%d",&n)!=EOF) { memset(g,63,sizeof(g)); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&g[i][j]); if(g[i][j]==-1) g[i][j]=INF; } } for(int i=1;i<=n;i++) scanf("%d",&b[i]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) fa[i][j]=j; for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(g[i][j]>g[i][k]+g[k][j]+b[k]) { g[i][j]=g[i][k]+g[k][j]+b[k]; fa[i][j]=fa[i][k]; } else if(g[i][j]==g[i][k]+g[k][j]+b[k]) { fa[i][j]=min(fa[i][j],fa[i][k]); } } } } int s,t; while(scanf("%d%d",&s,&t)!=EOF) { if(s==-1&&t==-1) break; printf("From %d to %d :\nPath: ",s,t); int next=fa[s][t]; printf("%d",s); if(s!=t) { while(next!=t) { printf("-->"); printf("%d",next); next=fa[next][t]; } printf("-->%d",t); } printf("\nTotal cost : %d\n\n",g[s][t]); } } return 0;}