标签:ios iostream cout using 实现 std ace log for
思路:
状压dp。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 #define N 16 6 #define INF 0x3f3f3f3f 7 int map[N][N]; 8 int dp[1 << N][N]; 9 int rec(int S, int v, int n) 10 { 11 if(dp[S][v] >= 0) 12 return dp[S][v]; 13 if(S == (1 << n) - 1 && v == n - 1) 14 { 15 return dp[S][v] = 0; 16 } 17 int res = INF; 18 for(int i = 0; i < n; i++) 19 { 20 if(!(S >> i & 1)) 21 { 22 res = min(res, rec(S | 1 << i, i, n) + map[v][i]); 23 } 24 } 25 return dp[S][v] = res; 26 } 27 int main() 28 { 29 memset(dp, -1, sizeof(dp)); 30 int n; 31 scanf("%d", &n); 32 for(int i = 0; i < n; i++) 33 { 34 for(int j = 0; j < n; j++) 35 { 36 scanf("%d",&map[i][j]); 37 if(j == 0 && i != 0) 38 { 39 map[i][j] = INF; 40 } 41 } 42 } 43 cout << rec(0, 0, n) << endl; 44 return 0; 45 }
标签:ios iostream cout using 实现 std ace log for
原文地址:http://www.cnblogs.com/wangyiming/p/6347848.html