URAL 1152. False Mirrors(DP)
生活随笔
收集整理的這篇文章主要介紹了
URAL 1152. False Mirrors(DP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接
理解了題意之后,就不難了。。狀態壓縮+暴力.
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\n",dp[0]); 56 return 0; 57 }?
轉載于:https://www.cnblogs.com/naix-x/p/3298458.html
總結
以上是生活随笔為你收集整理的URAL 1152. False Mirrors(DP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET:动态代理的 “5 + 1” 模
- 下一篇: xml入门简介--两天学会xml