天天看點

HDOJ 1385 Minimum Transport Cost 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 description

these 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.

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

output

from c to d :

path: c-->c1-->......-->ck-->d

total cost : ......

......

from e to f :

path: e-->e1-->..........-->ek-->f

note: if there are more minimal paths, output the lexically smallest one. print a blank line after each test case.

sample input

sample output

source

最基本的floyd最短路,難點在記錄字典序最小的路徑.....

有兩種路徑記錄方法:

出始化: fa[a][b]=a 則 fa[a][b]=c 表示  a....c->b

出始化: fa[a][b]=b 則 fa[a][b]=c 表示  a->c....b

根據題意,要選擇第二種