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
- HDU 5816 Hearthstone(狀態壓縮DP+概率)
- hdu--5418Victor and World+狀態壓縮DP
- bzoj4145: [AMPPZ2014]The Prices(狀態壓縮+Dp)
- 狀態壓縮
- 動態規劃——用二進位制表示集合的狀態壓縮DP動態規劃
- 狀壓 dp
- 狀壓DP
- [狀壓dp] 最短Hamilton路徑(模板題+狀壓dp)
- HDU 3006 The Number of set (狀態壓縮)
- 論文閱讀 狀態壓縮
- 簡單好用的js 壓縮工具JS
- POJ3279 Fliptile【狀態壓縮+DFS】
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- Word檔案太大怎麼壓縮,分享壓縮Word的簡單方法
- 怎麼把影片壓縮?實用又簡單的壓縮影片方法
- 動態規劃中初識狀態壓縮(入門)動態規劃
- 批次壓縮影片大小的簡單操作分享
- POJ 2777 Count Color (線段樹+狀態壓縮)
- HDU 1074 Doing Homework(狀壓DP)
- 狀壓DP基礎入門
- 【Node】簡單快捷的圖片壓縮指令碼指令碼
- Hive的壓縮儲存和簡單優化Hive優化
- 合理安排(狀壓dp,包括技巧)
- 手機將PDF檔案壓縮的簡單方法
- 簡單聊聊 GZIP 的壓縮原理與日常應用
- 簡單實用的mac壓縮軟體:iZip for MacMac
- Hadoop上配置snappy壓縮格式(最簡單的做法)HadoopAPP
- 三種簡單的PDF檔案快速壓縮方法
- 簡單瞭解一下壓縮表
- 【leetcode 簡單】 第一百零六題 壓縮字串LeetCode字串
- NOI2001 炮兵陣地(狀壓dp)
- E - Remove Pairs(狀壓dp+博弈論)REMAI
- 記錄一個很簡單的壓縮編碼--ADPCM
- 簡單實用的mac壓縮解壓軟體:iFastZip for Mac中文版MacAST
- vuex狀態管理簡單入門Vue
- Flutter狀態管理Provider,簡單上手FlutterIDE
- Keka Mac:高效簡潔的壓縮解壓工具Mac
- Luogu P1777 幫助 題解 [ 紫 ] [ 線性 dp ] [ 狀壓 dp ]