URAL 1152 False Mirrors(簡單的狀態壓縮dp)
20個數字,可以狀壓。簡單的狀態壓縮。狀態轉移方程式:dp[tai] = min(dp[tai] , dp[i]+w[tai]);列舉狀態求出最小值。
1152. False Mirrors
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Background
We wandered in the labyrinth for twenty minutes before finally entering the large hall. The walls were covered by mirrors here as well. Under the ceiling hung small balconies where monsters stood.
I had never seen this kind before. They had big bulging eyes, long hands firmly holding riffles and scaly, human-like bodies. The guards fired at me from the balconies, I shot back using my BFG-9000. The shot shattered three mirrors filling the room with silvery
smoke. Bullets drummed against my body-armor knocking me down to the floor. Falling down I let go a shot, and got up as fast as I fell down by rotating on my back, like I did in my youth while break dancing, all this while shooting three more times. Three
mirrors, three mirrors, three mirrors…
Sergey Lukjanenko, “The Labyrinth of Reflections”
Problem
BFG-9000 destroys three adjacent balconies per one shoot. (N-th balcony is adjacent to the first one). After the shoot the survival monsters inflict damage to Leonid (main hero of the novel)
— one unit per monster. Further follows new shoot and so on until all monsters will perish. It is required to define the minimum amount of damage, which can take Leonid.
Input
The first line contains integer N, аmount of balconies, on which monsters have taken a circular defense. 3 ≤ N ≤ 20. The second line contains N integers, amount of monsters
on each balcony (not less than 1 and no more than 100 on each).
Output
Output minimum amount of damage.
Sample
input | output |
---|---|
7 3 4 2 2 1 4 1 |
9 |
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
#define LL __int64
///#define LL long long
#define INF 0x3f3f3ff
#define PI 3.1415926535898
#define MOD 1000000009
const int maxn = 210;
using namespace std;
int dp[1<<22];
int w[1<<22];
int num[maxn];
int main()
{
int n;
while(cin >>n)
{
for(int i = 0; i < n; i++)
cin >>num[i];
for(int i = 0; i <= (1<<(n+1)); i++)
dp[i] = INF;
memset(w, 0 , sizeof(w));
for(int i = 0 ; i < 1<<n; i++)
{
for(int k = 0; k < n; k++)
{
if((i&(1<<k)) != 0)
w[i] += num[k];
}
}
dp[(1<<n)-1] = 0;
int tai;
int p = (1<<n)-1;
for(int i = (1<<n)-1; i >= 0; i--)
{
for(int j = 0; j < n; j++)
{
tai = i;
if((i&(1<<j)) != 0)
{
if(j == 0)
tai = tai&(p-(1<<(n-1)));
else tai = tai&(p-(1<<(j-1)));
tai = tai&(p-(1<<j));
if(j == n-1)
tai = tai&(p-1);
else
tai = tai&(p-(1<<(j+1)));
dp[tai] = min(dp[tai], dp[i]+w[tai]);
}
}
}
cout<<dp[0]<<endl;
}
return 0;
}
相關文章
- 演算法學習之路|狀態壓縮dp演算法
- NOIP2005過河[DP 狀態壓縮]
- URAL 1658. Sum of Digits(簡單dp)Git
- 動態規劃——用二進位制表示集合的狀態壓縮DP動態規劃
- 簡單的zip壓縮和解壓縮
- UVA 11825 dp、狀態壓縮、二進位制法表示集合
- 狀壓 dp
- 狀壓DP
- URAL 1577. E-mail(簡單二維dp)AI
- hdu3001 狀態壓縮dp+三進位制
- 論文閱讀 狀態壓縮
- 簡單好用的js 壓縮工具JS
- POJ3279 Fliptile【狀態壓縮+DFS】
- 狀態壓縮動態規劃 -- 炮兵陣地動態規劃
- HDU 5339 Untitled (狀態壓縮列舉)
- Word檔案太大怎麼壓縮,分享壓縮Word的簡單方法
- 怎麼把影片壓縮?實用又簡單的壓縮影片方法
- 動態規劃中初識狀態壓縮(入門)動態規劃
- hdu1074動態規劃狀態壓縮動態規劃
- 2014上海網路賽1004||hdu5045 contest【狀態壓縮dp】
- 批次壓縮影片大小的簡單操作分享
- HDU 4770 Lights Against Dudely(列舉所有狀態 當然壯壓dp會很簡單)AI
- 狀壓DP基礎入門
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- Hive的壓縮儲存和簡單優化Hive優化
- 【Node】簡單快捷的圖片壓縮指令碼指令碼
- 簡單實用的android 圖片的壓縮Android
- 合理安排(狀壓dp,包括技巧)
- 簡單聊聊 GZIP 的壓縮原理與日常應用
- 手機將PDF檔案壓縮的簡單方法
- 簡單實用的mac壓縮軟體:iZip for MacMac
- 三種簡單的PDF檔案快速壓縮方法
- 一個簡單的狀態列示例
- E - Remove Pairs(狀壓dp+博弈論)REMAI
- 2014上海網路賽1004||hdu5045 二分圖的最佳匹配 或 狀態壓縮dp
- 簡單實用的mac壓縮解壓軟體:iFastZip for Mac中文版MacAST
- vuex狀態管理簡單入門Vue
- Uva-1633 Dyslexic Gollum(狀壓DP)Go