理解了题意之后,就不难了。。状态压缩+暴力.
1 #include <cstring> 2 #include <cstdio> 3 #include <string> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <cmath> 9 using namespace std; 10 int dp[1<<20]; 11 int sum[1<<20]; 12 int p[101]; 13 int minz; 14 int main() 15 { 16 int i,n,j,t1,t2,x; 17 scanf("%d",&n); 18 for(i = 0;i < n;i ++) 19 { 20 scanf("%d",&p[i]); 21 } 22 for(i = 0;i < 1<<n;i ++) 23 { 24 dp[i] = 10000000; 25 for(j = 0;j < n;j ++) 26 { 27 if(i&(1<<j)) 28 sum[i] += p[j]; 29 } 30 } 31 dp[(1<<n)-1] = 0; 32 for(i = (1<<n)-1;i >= 1;i --) 33 { 34 for(j = 0;j < n;j ++) 35 { 36 if(i&(1<<j)) 37 { 38 x = i - (1<<j); 39 if(j-1 < 0) 40 t1 = n-1; 41 else 42 t1 = j-1; 43 if(j+1 >= n) 44 t2 = 0; 45 else 46 t2 = j+1; 47 if(x&(1<<t1)) 48 x -= 1<<t1; 49 if(x&(1<<t2)) 50 x -= 1<<t2; 51 dp[x] = min(dp[x],dp[i]+sum[x]); 52 } 53 } 54 } 55 printf("%d ",dp[0]); 56 return 0; 57 }