URAL 1152 False Mirrors(簡單的狀態壓縮dp)

畫船聽雨發表於2014-05-17

20個數字,可以狀壓。簡單的狀態壓縮。狀態轉移方程式:dp[tai] = min(dp[tai] ,  dp[i]+w[tai]);列舉狀態求出最小值。


1152. False Mirrors

Time limit: 2.0 second
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;
}


相關文章