Log inSign up
Problems / B102

Shipping route fare table

300 ptsCases 15Set by dongbinna
Added October 30.

Statement

A shipping company runs cargo routes between n ports. The ports are numbered 1 through n.

There are m routes in all. Each route has a departure port, an arrival port, and the fare for using that route once. A route sails in one direction only. A route from port A to port B does not mean there is a route from port B to port A as well.

There may be several routes joining the same two ports. The fares can differ from route to route in that case, so any one of them may be picked and used.

Cargo can pass through any number of ports on the way. The fare for sending cargo from one port to another is the fares of every route it passed through all added together. The company wants to build a table holding, for each port, the smallest fare for sending cargo to every other port.

Write a program that calculates, for every pair of ports, the smallest fare for sending cargo from port A to port B. If there is no way at all from port A to port B, write 0 in that place. Write 0 in the place where A and B are the same port as well.

For example, suppose n=5 and m=8. Let the information about each route be as in the table below.

Route12345678
Departure port51411441
Arrival port33145214
Fare29349883096544921527284724258523443709

Route 3 and route 7 both go from port 4 to port 1, and their fares are 654 and 85234. The cheaper 654 is the one to use. Route 4 and route 8 likewise both go from port 1 to port 4, and the cheaper 43709 is the one to use.

Not a single route leaves port 2 or port 3. So no cargo can be sent from those two ports to any other port.

Laying out the smallest fare for every pair of ports in this example gives the table below.

Departure portTo port 1To port 2To port 3To port 4To port 5
Port 10116134566334370927284
Port 200000
Port 300000
Port 46547242557287027938
Port 5002934900

Look at a few of the places in the table. The fare of route 2, which goes straight from port 1 to port 3, is 88309. But using the 27284 from port 1 to port 5 and then the 29349 from port 5 to port 3 adds up to 56633, which is cheaper. The only way from port 1 to port 2 runs through port 4, which is 43709 added to 72425, so 116134.

Input and Output Examples

Example 1

Input

5
8
5 3 29349
1 3 88309
4 1 654
1 4 49215
1 5 27284
4 2 72425
4 1 85234
1 4 43709

Output

0 116134 56633 43709 27284 
0 0 0 0 0 
0 0 0 0 0 
654 72425 57287 0 27938 
0 0 29349 0 0 

Example 2

Input

3
3
2 3 82322
2 3 40264
2 3 18432

Output

0 0 0 
0 0 18432 
0 0 0 

Example 3

Input

1
0

Output

0 

Input

  • First the number of ports n is given on the first line. n is a natural number from 1 to 100.

  • Next the number of routes m is given on the second line. m is an integer from 0 to 100,000.

  • Then m lines follow, giving the information about each route. One line holds the departure port number A, the arrival port number B and the fare C, separated by spaces. A and B are different natural numbers from 1 to n, and C is a natural number from 1 to 100,000. If m is 0, none of these lines is given.

Output

  • Print n lines. Line A holds the smallest fare for sending cargo from port A to port 1, to port 2, and on in order to port n, printed in that order separated by spaces. Print 0 in a place where there is no way from port A to port B, and print 0 in the place where A and B are the same as well.

Requirements

  • A route sails in one direction only
  • There may be several routes joining the same two ports. Use the cheapest of them
  • There is no limit on the number of ports passed through on the way
  • Write 0 in a place with no way and in a place between a port and itself
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
  • There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 15 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
1
0
0
3
3
2 3 82322
2 3 40264
2 3 18432
0 0 0
0 0 18432
0 0 0
5
8
5 3 29349
1 3 88309
4 1 654
1 4 49215
1 5 27284
4 2 72425
4 1 85234
1 4 43709
0 116134 56633 43709 27284
0 0 0 0 0
0 0 0 0 0
654 72425 57287 0 27938
0 0 29349 0 0

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 61ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 3 public cases. Submitting runs the hidden cases too.

People who solved this problem

Nobody yet. You could be the first.