#P7751. [COCI 2013/2014 #2] PUTNIK

[COCI 2013/2014 #2] PUTNIK

Description

Salesman Sept has a task: to visit NN cities (numbered from 11 to NN), and each city must be visited exactly once.

Among these NN cities, there is a flight between every pair of cities. Sept values efficiency, so he wants to minimize the total time spent flying. For this, he may change the visiting order of the cities.

However, Sept has a strange requirement for the visiting order:

  • There is no requirement for city 11.
  • If he is going to visit city K(K[2,N])K(K\in[2,N]), then all cities with numbers smaller than KK must be visited either all before visiting city KK, or all after visiting city KK.
    In other words, for two cities X,Y(1X,Y<K)X,Y(1\le X,Y< K), it is not allowed that city XX is visited before city KK while city YY is visited after city KK.

Under these restrictions, given the time needed to take flights between every pair of cities, find the minimum total flying time Sept spends.

Input Format

The first line contains an integer NN, the number of cities.

The next NN lines each contain NN integers. Let (A,B)(A,B) denote the BB-th number in the AA-th line. Then:

  • (A,B)(A,B) is the time needed to take a flight between cities AA and BB.
  • If A=BA=B, then (A,B)=0(A,B)=0.
  • (A,B)=(B,A)(A,B)=(B,A).

Output Format

Output one integer in a single line, the minimum total flying time Sept spends.

3 
0 5 2 
5 0 4 
2 4 0
7
4 
0 15 7 8 
15 0 16 9 
7 16 0 12 
8 9 12 0
31

Hint

Explanation of Sample 1

The order 2,1,3\tt2,1,3 or 3,1,2\tt3,1,2 is valid.

Note that 1,3,2\tt1,3,2 takes less time, but it does not satisfy Sept's strange requirement.

Explanation of Sample 2

The order 3,1,2,4\tt3,1,2,4 or 4,2,1,3\tt4,2,1,3 is valid.

Constraints

This problem does not use bundled testdata, and subtasks will not be shown in judging.

  • Subtask 1 (40pts)\tt(40pts): 2N102\le N\le 10.
  • Subtask 2 (20pts)\tt(20pts): 2N202\le N\le 20.
  • Subtask 3 (60pts)\tt(60pts): no special restrictions.

For 100%100\% of the data, 2N15002\le N\le 1500, 0(A,B)1030\le (A,B)\le 10^3.

Source

This problem is translated from COCI2013-2014 CONTEST 2 T4 PUTNIK.

According to the original testdata setup, the full score of this problem is 120120 points.

Translated by ChatGPT 5