标签:16px return algo tmp alt algorithm 准备 lin 格式
3
0 10 1
3 0 5
1 2 0
1
2
7
花园被分为3个地区,在2号地区有能量块,飘飘乎居士行走的路线如下
1->3->2->1->3
行走的总路程为7,也就是最后的答案。
题解:
裸状压。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <iostream> 5 #include <algorithm> 6 #define max(a, b) a > b ? a : b 7 #define min(a, b) a < b ? a : b 8 9 inline void read(int &x) 10 { 11 x = 0;char ch = getchar();char c = ch; 12 while(ch > ‘9‘ || ch < ‘0‘)c = ch, ch = getchar(); 13 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 14 if(c == ‘-‘)x = -x; 15 } 16 17 const int MAXN = 100 + 10; 18 const int MAXP = 10 + 5; 19 20 int g[MAXN][MAXN], n, p, dir[MAXP], ok1, ok2; 21 int f[MAXN][1 << MAXP]; 22 23 int main() 24 { 25 memset(f, 0X3f, sizeof(f)); 26 read(n); 27 for(register int i = 1;i <= n;++ i) 28 for(register int j = 1;j <= n;++ j) 29 read(g[i][j]); 30 for(register int k = 1;k <= n;++ k) 31 for(register int i = 1;i <= n;++ i) 32 for(register int j = 1;j <= n;++ j) 33 g[i][j] = min(g[i][j], g[i][k] + g[k][j]); 34 read(p); 35 for(register int i = 1;i <= p;++ i) 36 { 37 read(dir[i]); 38 if(dir[i] == 1) ok1 = true; 39 else if(dir[i] == n) ok2= true; 40 } 41 if(!ok1) dir[++ p] = 1; 42 if(!ok2) dir[++ p] = n; 43 44 std::sort(dir + 1, dir + 1 + p); 45 f[1][1] = 0; 46 //f[i][S]表示从1出发走到点i,取到了S的能量块的最短路径 47 register int M = 1 << p, now, tmp; 48 for(register int S = 1;S < M;++ S) 49 { 50 for(register int i = 1;i <= p;++ i) 51 { 52 if(!(S & (1 << (i - 1))))continue; 53 now = dir[i]; 54 for(register int j = 1;j <= p;++ j) 55 { 56 if(i == j)continue; 57 if(!(S & (1 << (j - 1))))continue; 58 tmp = dir[j]; 59 f[now][S] = min(f[now][S], f[tmp][S ^ (1 << (i - 1))] + g[tmp][now]); 60 } 61 } 62 } 63 printf("%d", f[n][M - 1]); 64 return 0; 65 }
标签:16px return algo tmp alt algorithm 准备 lin 格式
原文地址:http://www.cnblogs.com/huibixiaoxing/p/7269657.html