Submit solution


Points: 0.03 (partial)
Time limit: 0.75s
Memory limit: 512M
Input: stdin
Output: stdout

Problem source:
special one
Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

Trong kì nghỉ hè năm nay Sherry được bố thưởng cho một tour du lịch quanh ~N~ đất nước tươi đẹp với nhiều thắng cảnh nổi tiếng (vì Sherry rất ngoan). Tất nhiên Sherry sẽ đi bằng máy bay.

Giá vé máy bay từ đất nước ~i~ đến đất nước ~j~ là ~C_{ij}~ (dĩ nhiên ~C_{ij}~ có thể khác ~C_{ji}~). Tuy được bố thưởng cho nhiều tiền để đi du lịch nhưng Sherry cũng muốn tìm cho mình một hành trình với chi phí rẻ nhất có thể để dành tiền mua quà về tặng mọi người (các chuyến bay của Sherry đều được đảm bảo an toàn tuyệt đối).

Bạn hãy giúp Sherry tìm một hành trình đi qua tất cả các nước, mỗi nước đúng một lần sao cho chi phí là bé nhất nhé.

Input

Dòng đầu tiên: ~N~ ~(5 < N < 16)~.

Dòng thứ ~i~ trong ~N~ dòng tiếp theo: Gồm ~N~ số nguyên, số thứ ~j~ là ~C_{ij}~ ~(0 < C_{ij} < 10001)~.

Output

Gồm một dòng duy nhất ghi chi phí bé nhất tìm được.

Sample Input

6
0 1 2 1 3 4 
5 0 3 2 3 4 
4 1 0 2 1 2 
4 2 5 0 4 3 
2 5 3 5 0 2 
5 4 3 3 1 0

Sample Output

8

Comments

Please read the guidelines before commenting.



  • 3
    vominhmanh10  commented on Nov. 16, 2025, 5:59 a.m. edit 2

    dp bit mask các trạng thái là đường đi từ u (với mask có u) và đi tới v => dp[mask][v] = chi phí nhỏ nhất khi đã đi tới đỉnh v
    khởi tất cả là inf, trường hợp cơ sở là đỉnh bắt đầu thăm (mask == 1 << v) chi phí là 0
    khi đi từ u (mask có u không có v) tới v (mask | (1 << v)): dp[mask | (1 << v)][v] = min(... dp[mask][u] + C[u][v])
    thực hiện trong O(n^2 * 2^n)

    import sys
    input = sys.stdin.readline
    
    def main():
        n = int(input())
        a = [list(map(int, i.split())) for i in sys.stdin.readlines()]
        mask_all = 1 << n
        inf = 10**18
        dp = [[inf] * n for _ in range(mask_all)]
        res = inf
        for v in range(n): dp[1 << v][v] = 0
        for mask in range(mask_all):
            for u in range(n):
                if mask & (1 << u) and dp[mask][u] != inf:
                    for v in range(n):
                        if not (mask & (1 << v)) and u != v:
                            ans = mask | (1 << v)
                            dp[ans][v] = min(dp[ans][v], dp[mask][u] + a[u][v])
        return min(dp[(1 << n) - 1])
    print(main())
    

  • -2
    phanhoang  commented on Nov. 15, 2025, 1:45 p.m.

    nbsdfkbakfkr;hregreghgt


  • -12
    phannam310810  commented on June 4, 2025, 6:09 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -19
    khanhlani  commented on Aug. 20, 2024, 2:06 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -10
    Soda12  commented on Aug. 16, 2024, 4:49 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -20
    chunguyen2k8  commented on March 27, 2024, 8:43 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • -15
      pinkkiu  commented on April 14, 2024, 8:59 a.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


  • -13
    chunguyen2k8  commented on March 23, 2024, 4:18 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -25
    anhminhlabo  commented on Sept. 30, 2023, 1:03 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • 117
    PPAP_1264589  commented on Aug. 29, 2021, 7:47 a.m.

    Giải thích test đề bài: 1 hành trình có thể với chi phí thấp nhất

    3 -> 6 -> 5 -> 1 -> 2 -> 4

    0 1 2 1 3 4

    5 0 3 2 3 4

    4 1 0 2 1 2

    4 2 5 0 4 3

    2 5 3 5 0 2

    5 4 3 3 1 0