Log inSign up
Problems / B060

Island tour

300 ptsCases 16Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

There are N islands in the sea, numbered 1 through N. A supply ship starts from island 1, visits every other island once, and then has to come back to island 1. An island it has visited is never visited again.

Two islands may have a sea route between them, or they may not. The fuel a route takes can also differ with the direction of travel. Because of the current, the fuel from island i to island j and the fuel from island j to island i can be different.

Write a program that picks the order in which the supply ship visits the islands so that the total fuel for one round trip is as small as possible, and finds that total.

For example, suppose N=4 and the fuel between the islands is as in the table below. Each row is the island the ship leaves from, and each column is the island it arrives at.

Island left fromTo 1To 2To 3To 4
Island 10101520
Island 250910
Island 3613012
Island 48890

There are 6 orders that start from island 1, visit the other three islands once each and come back, and the total fuel for each order is in the table below.

Visiting orderTotal fuel
1, 2, 3, 4, 139
1, 2, 4, 3, 135
1, 3, 2, 4, 146
1, 3, 4, 2, 140
1, 4, 2, 3, 143
1, 4, 3, 2, 147

Follow the second order. Island 1 to island 2 takes 10, island 2 to island 4 takes 10, island 4 to island 3 takes 9, and island 3 to island 1 takes 6, which comes to 35. That is the smallest of the six, so the answer in this example is 35.

A 0 in the table means there is no sea route in that direction. A direction with no route cannot be traveled, so an order that uses that direction drops out of the count entirely. At least one order that visits every island once and comes back to island 1 exists.

Input and Output Examples

Example 1

Input

4
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0

Output

35

Example 2

Input

2
0 824230
864553 0

Output

1688783

Example 3

Input

5
0 26 37 19 0
50 0 12 0 64
87 79 0 33 26
39 0 67 0 23
0 46 37 48 0

Output

151

Input

  • First the number of islands N is given as a natural number. N is a natural number from 2 to 10.

  • Next the fuel between the islands is given over N lines. The j-th number on the i-th line is the fuel it takes to go from island i to island j, and the numbers on a line are separated by spaces. A value of 0 means there is no sea route in that direction. So the i-th number on the i-th line is always 0. A value other than 0 is a natural number smaller than 1,000,000.

Output

  • Print the smallest total fuel for one round trip of the supply ship on the first line, as a natural number.

  • Only one newline follows the number you print. Do not put a space before or after the number, and do not add a blank line after that newline.

Requirements

  • The ship starts from island 1, visits every island once and comes back to island 1
  • An island it has visited is never visited again
  • The fuel it takes can differ with the direction of travel
  • A direction whose value is 0 cannot be traveled
  • At least one order that visits every island once and comes back exists
  • 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 16 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 16 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 16 cases to solve this.

Input
Expected output
4
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
35
2
0 824230
864553 0
1688783
5
0 26 37 19 0
50 0 12 0 64
87 79 0 33 26
39 0 67 0 23
0 46 37 48 0
151

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 32ms

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

The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.

1bampal2119ms759 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.