hdu1385 Minimum Transport Cost

Source: Internet
Author: User

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 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:21 from 3 to 5:P ath:3-->4-->5total cost:16 Fr Om 2 to 4:P ath:2-->1-->5-->4total cost:17 The main problem, truck freight to a place need tolls and taxes, ask you how to get the least money, and the output is the shortest way to go
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 #defineM 1006 #defineINF 0XFFFFFF7 intMap[m][m];8 intDis[m][m],n,b[m];9 intMain ()Ten { One     inti,j; A     voidFloyd (); -      while(cin>>n,n) -     { the          for(i=1; i<=n; i++) -              for(j=1; j<=n; J + +) -             { -Cin>>Map[i][j]; +                 if(map[i][j]==-1) -map[i][j]=inf; +             } A          for(i=1; i<=n; i++) atCin>>B[i]; - Floyd (); -         intx, y; -          while(cin>>x>>y,x!=-1) -         { -printf"From %d to%d: \npath:", x, y); in             intnext=x; -              while(next!=y) to             { +printf"%d-->", next); -next=Dis[next][y]; the             } *printf"%d\ntotal Cost:%d\n\n", Y,map[x][y]); $         }Panax Notoginseng     } -     return 0; the } + voidFloyd () A { the     inti,j,k; +      for(i=1; i<=n; i++) -          for(j=1; j<=n; J + +) $dis[i][j]=J; $      for(k=1; k<=n; k++) -          for(i=1; i<=n; i++) -         { the             if(map[i][k]!=inf) -                  for(j=1; j<=n; J + +)Wuyi                 { the                     int_next=map[i][k]+map[k][j]+B[k]; -                     if(map[i][j]>_next) Wu                     { -map[i][j]=_next; Aboutdis[i][j]=Dis[i][k]; $                     } -                     Else if(Map[i][j]==_next) dis[i][j]=min (dis[i][j],dis[i][k]); -                 } -         } A}

hdu1385 Minimum Transport Cost

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.