Minimum Transport Cost
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 9052 Accepted Submission (s): 2383
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
...
E
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 are 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 Input50 3 22-1 0 5-1 -122 5 0 9 20-1-1 9 0 44-1 4 8 3 one-4-1 -10 Sample outputfrom 1 to 3:P a Th:1-->5-->4-->3total cost:21 from 3 to 5:P ath:3-->4-->5total cost:16 from 2 to 4:P ath:2-->1-- >5-->4total cost:17 Sourceasia 1996, Shanghai (Mainland China) test instructions, to a map of n*n, 1 means non-connected, positive integers for the edge, each point a bit right, given the beginning S and the end point T, The cost of S to T is to pass the Benquan and add the path except S and T on the point weights and, to find the shortest path of s to T and output the route idea: Floyd,path[i][j] represents the first point on the I-to-J path
/*id:linkarftcprog:1385.cpplang:c++*/#include<map>#include<Set>#include<cmath>#include<stack>#include<queue>#include<vector>#include<cstdio>#include<string>#include<utility>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1e-8#defineRandin Srand ((unsigned int) time (NULL))#defineInput freopen ("Input.txt", "R", stdin)#defineDebug (s) cout << "s =" << s << endl;#defineOutstars cout << "*************" << Endl;Const DoublePI = ACOs (-1.0);Const DoubleE = exp (1.0);Const intINF =0x3f3f3f3f;Const intINF =0x7fffffff; typedefLong Longll;Const intMAXN = -;intMP[MAXN][MAXN];intPATH[MAXN][MAXN];//Path[i][j] Record the next point from I to JintTAX[MAXN];intN, S, T;voidFloyd () { for(intK =1; K <= N; K + +) { for(inti =1; I <= N; i + +) { for(intj =1; J <= N; J + +) { if(i = = k | | j = = k)Continue; intTMP = Mp[i][k] + mp[k][j] +Tax[k]; if(Mp[i][j] >tmp) {Mp[i][j]=tmp; PATH[I][J]=Path[i][k]; } Else if(Mp[i][j] = =tmp) {Path[i][j]=min (path[i][j], path[i][k]); } } } }}voidPrint_path () {printf ("Path:%d", s); intCur =s; while(cur! =t) {cur=Path[cur][t]; printf ("-->%d", cur); } printf ("\ n");}intMain () { while(~SCANF ("%d", &n) &&N) { for(inti =1; I <= N; i + +) { for(intj =1; J <= N; J + +) {scanf ("%d", &Mp[i][j]); if(Mp[i][j] = =-1) mp[i][j] = inf;//It's best to replace it with an INF, with a-1 sentence that's easy to mistake. ElsePATH[I][J] =J; } } for(inti =1; I <= N; i + +) scanf ("%d", &Tax[i]); Floyd (); while(~SCANF ("%d%d", &s, &t)) {if(s = =-1&& T = =-1) Break; printf ("From %d to%d: \ n", S, t); Print_path (); printf ("Total Cost:%d\n\n", mp[s][t]); } } return 0;}
HDU1385 Minimum Transport Cost