HDU 1385 Minimum Transport cost (Floyd record Path)

Source: Internet
Author: User
Tags printf sort


Minimum Transport Cost Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 10562 Accepted Submission (s): 2931


Problem Description These 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.

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 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:

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 is 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


1 23

Test instructions

There are n cities that give the adjacency matrix between these cities, and the Matrix-1 means that two cities are connected without roads, and other values represent path lengths.

If a car passes through a city, it must pay a certain amount of money (probably tolls).

Now, from City A to City B, spend the sum of the length of the route, plus the tolls of all cities except the starting and ending points.

Minimum cost, and if more than one path is met, the path to the smallest dictionary order is output.

Note the dictionary sort!


dictionary Sort if the path of 123 and 13 is the same but 123 is less than 13 so output 123


here is the code

using an array followed by the output to Floyd subsequent record nodes .

or the output node is inverted with stack stacks.


/* Floyd template Stack record path wrong causes the array AC */#include <iostream> #include <stdio.h> #include <stack> #includ
E<string.h> using namespace std;

#define N-Th const int inf=1<<29;
int map[n][n];
int pre[n][n];
int b[n];
int n;
	void Floyd () {int i,j,k; for (k=1;k<=n;k++) {to (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (Map[i][j]> (map[i][k]+map[k][j]+b
					[K])) {map[i][j]=map[i][k]+map[k][j]+b[k]; pre[i][j]=pre[i][k];//successor} else if (map[i][j]== (Map[i][k]+map[k][j]+b[k]) &&pre[i][j]>pre[i][k]) p RE[I][J]=PRE[I][K];
	Handle Dictionary Sort}}}} int main () {int m;
	int i,j;
		while (~SCANF ("%d", &n), N) {memset (pre,-1,sizeof (pre));
				for (i = 1; I <= n; i + +) {for (j = 1; J <= N; j + +) {scanf ("%d", &map[i][j]);
				if (map[i][j] = =-1) map[i][j] = inf;			
			else pre[i][j] = j;
		}} for (i = 1; I <= n; i + +) scanf ("%d", &b[i]);
		Floyd ();
int x, y; for (i=1;i<=n;i++)//{//for (j=1;j<=n;j++)//printf ("%d", pre[i][j]);			
printf ("\ n"); } while (scanf ("%d%d", &x, &y), (x! =-1 | | Y! =-1)) {//stack<int> Q;//int NOW=PR
E[Y][X];
Q.push (y);
while (1)//{//Q.push (now),//if (now== x)//break;///NOW=PRE[NOW][X];//}//Q.pop ();
printf ("From%d to%d: \ n", x, y);
printf ("Path:%d", x);
if (x==y)//printf ("\ n"); else//{//while (!
Q.empty ())//{//printf ("-->%d", Q.top ());//Q.pop ();//}//printf ("\ n");
			}//printf ("Total Cost:%d\n\n", Map[x][y]);
			printf ("From%d to%d: \ n", x, y);
			printf ("Path:");
			printf ("%d", x);
			if (x = = y) printf ("\ n"); 
				else {int next = Pre[x][y];
					while (next! = y) {printf ("-->%d", next);
				Next = Pre[next][y];
			} printf ("-->%d\n", y);	
		} printf ("Total Cost:%d\n\n", Map[x][y]);
}} return 0; }





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.