Minimum Transport CostTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8083 Accepted Submission (s): 2104
Problem Descriptionthese is N cities in Spring country. Between each pair of cities there is one transportation track or none. Now there are some cargo that should being delivered from the one city to another. The transportation fee consists of the parts:
The cost of the transportation on the path between these cities, and
A certain tax which'll 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 have 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 was 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 are no direct path between city I and City J. Bi represents the passing through city I. And the cargo is to being 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 is 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:P ath:1-->5-->4-->3total Cost:21from 3 to 5:P ath:3-->4-->5total Cost:16from 2 to 4:P Ath : 2-->1-->5-->4total cost:17#include <stdio.h> #include <queue>using namespace std;const int N =105;const int inf = 999999999;struct path{ int K,path[n],dis;}; int n,mapt[n][n],dis[n],frome[n],b[n]; PATH p[n][n];bool Compare (int x,int y) {int k[2]={0},a[2][n]; while (frome[x]!=x) {a[0][k[0]++]=x; X=FROME[X]; } a[1][k[1]++]=x; while (frome[y]!=y) {a[1][k[1]++]=y; Y=frome[y]; } while (K[1]&&k[0]) {k[0]--; k[1]--; if (A[0][k[0]]>a[1][k[1]]) return true; else if (A[0][k[0]]<a[1][k[1]]) return false; } if (K[0]) return true; return false;} void Spfa () {queue<int>q; int inq[n]={0},s; for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) dis[j]=inf,frome[j]=j; dis[i]=0; Q.push (i); while (!q.empty ()) {S=q.front (); Q.pop (); inq[s]=0; for (int j=1;j<=n;j++) if (mapt[s][j]!=-1&&j!=s) {if (Dis[j]>dis[s]+mapt[s][j]+b[j]) {Dis[j]=dis[s]+mapt[s][j]+b[j] ; Frome[j]=s; if (!inq[j]) Inq[j]=1,q.push (j); } else if (Dis[j]==dis[s]+mapt[s][j]+b[j]) {if (compare (J,s)) Frome[j]=s; }}} for (int j=1;j<=n;j++) {p[i][j].k=0; P[I][J].DIS=DIS[J]; if (j!=i) p[i][j].dis-=b[j]; int &k=p[i][j].k; if (I!=J)//Start and end point, only one point p[i][j].path[k++]=j is output; S=FROME[J]; while (S!=frome[s]) {p[i][j].path[k++]=s; s=frome[s]; } p[i][j].path[k++]=i; }}}int Main () {int a,aa; while (scanf ("%d", &n) >0&&n) {for (Int. i=1;i<=n;i++) for (int j=1;j<=n;j++) scanf ("%d", &mapt[i][j]); for (int i=1;i<=n;i++) scanf ("%d", &b[i]); SPFA (); while (scanf ("%d%d", &a,&aa) >0&&a+aa>0) {printf (' from%d to%d: \ n ', A,AA); printf ("Path:%d", a); int k=p[a][aa].k-1; while (k>0) {k--; printf ("-->%d", P[a][aa].path[k]); } printf ("\ntotal Cost:%d\n\n", P[a][aa].dis); } }}
Hdu1385minimum Transport Cost (min. spfa+ Output Dictionary tree path between two points)