*HDU 1385 Shortest Path

Source: Internet
Author: User

Minimum Transport Cost

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 10183 Accepted Submission (s): 2791


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 Input50 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 Outputfrom 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

Source Asia 1996, Shanghai (Mainland China) test instructions: There is a fuel charge from one city to another and every city has to pay tolls, starting point and end point, asking for the least cost from the starting point to the end point. and print the path. Code:
1 //Floyd records the path with a path array. 2#include <iostream>3#include <cstdio>4#include <cstring>5 using namespacestd;6 intmp[1003][1003];7 intfei[1003],path[1003][1003];8 intMain ()9 {Ten     intn,a,b,t; One      while(SCANF ("%d", &n) &&N) A     { -          for(intI=1; i<=n;i++) -          for(intj=1; j<=n;j++) the         { -scanf"%d",&mp[i][j]); -             if(mp[i][j]==-1) -mp[i][j]=10000007; +Path[i][j]=j;//Initialize Path -         } +          for(intI=1; i<=n;i++) Ascanf"%d",&fei[i]); at          for(intk=1; k<=n;k++) -          for(intI=1; i<=n;i++) -          for(intj=1; j<=n;j++) -         { -             if(Mp[i][j]>mp[i][k]+mp[k][j]+fei[k])//plus tax. -             { inmp[i][j]=mp[i][k]+mp[k][j]+Fei[k]; -path[i][j]=Path[i][k]; to             } +             Else if(mp[i][j]==mp[i][k]+mp[k][j]+fei[k]&&path[i][j]>Path[i][k]) -path[i][j]=Path[i][k]; the         } *          while(SCANF ("%d%d",&a,&b)) $         {Panax Notoginseng             if(a<0&&b<0) Break; -printf"From %d to%d: \ n", A, b); the             intx=A; +printf"Path:%d", a); A              while(x!=b) the             { +printf"-->%d", Path[x][b]); -x=Path[x][b]; $             } $printf"\ n"); -printf"Total Cost:%d\n\n", Mp[a][b]); -         } the     } -     return 0;Wuyi}

*HDU 1385 Shortest Path

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.