Worst path P10051


Statement
 

pdf   zip

Given a directed and complete graph with nn vertices, and an initial vertex xx, compute the maximum cost of all the paths without repeated vertices that begin at xx. The given graph is represented by an n×nn \times n matrix MM, where for every pair (i,j)(i, j) with iji \ne j, mijm_{ij} is the (perhaps negative) cost of the arc from ii to jj.

For instance, the maximum cost of the first test is 80, corresponding to the path 1031 \to 0 \to 3, with cost 10+90=80-10 + 90 = 80.

Input

Input consists of several cases, each one with the number of vertices nn, followed by the matrix MM (nn lines, each one with nn integer numbers), followed by the initial vertex xx. Vertices are numbered from 0 to n1n-1. You can assume 1n181 \le n \le 18, 0x<n0 \le x < n, that the diagonal has only zeros, and that the rest of numbers are between 106-10^6 and 10610^6.

Output

For every case, print the cost of the worst path without repeated vertices that begins at xx.

Public test cases
  • Input

    4
      0 -10  30  90
    -10   0  50 -12
    -60  35   0  15
     14 -70 -11   0
    1
    
    1
    0
    0
    
    3
     0  6  8
    -4  0  3
    -7 -2  0
    2
    

    Output

    80
    0
    0
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++