Tags
搜索,状压?。
裸的旅行商问题
#include <stdio.h> #include <string.h> #define re register #define GC getchar() #define Clean(X,K) memset(X,K,sizeof(X)) #define U unsigned #define Min(X,Y) (X<Y?X:Y) short Qread () { short X = 0 ; char C = GC ; while (C > ‘9‘ || C < ‘0‘) C = GC ; while (C >=‘0‘ && C <=‘9‘) { X = X * 10 + C - ‘0‘ ; C = GC ; } return X ; } const short Maxn = 20 , INF = (1 << 15) - 1; U short int A[Maxn][Maxn] , N ; short F[Maxn][1 << Maxn]; void DFS (short Now , U X) { if (F[Now][X] != -1) return ; F[Now][X] = INF; for (re int i = 0 ; i < N; ++ i) { if ((X & (1 << i)) && Now != i) { DFS (i , X & (~ (1 << Now))) ; F[Now][X] = Min (F[Now][X] , A[i][Now] + F[i][X & (~ (1 << Now))]) ; } } } int main () { //freopen ("P1171.in" , "r" , stdin) ; N = Qread () ; for (re int i = 0 ; i < N; ++ i) for (re int j = 0 ; j < N; ++ j) A[i][j] = Qread () ; Clean (F , -1) ; F[0][1] = 0 ; for (re int i = 0 ; i < N; ++ i) DFS (i , (1 << N) - 1) ; int Ans = INF ; for (re int i = 0 ; i < N; ++ i) Ans = Min (Ans ,A[i][0] + F[i][(1 << N) - 1]) ; printf ("%d\n" , Ans) ; fclose (stdin) , fclose (stdout) ; return 0 ; }
原文地址:https://www.cnblogs.com/bj2002/p/10588204.html
时间: 2024-11-10 18:00:19