HDU 3377 Plan

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3377


題意:左上角出發走到右下角結束,每個格子有個分數。每個格子最多經過一次,可以不經過,求最大得分。


思路:這題要求一個簡單路徑,也就是說只有起點和終點是單插頭,其餘點都是雙插頭, 所以分類討論一下轉移即可。


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define Clean(x,y) memset(x,y,sizeof(x))
int n,m;
int pre,cur;

int g[20][20];
const int maxn = 1009999;

int bit = 7;
int inc = 3;
int code[20];
int vis[20];

struct hash_table
{
    int head[10007] , next[maxn];
    LL value[maxn] , state[maxn];
    int size;
    void clear()
    {
        size = 0;
        Clean(head,-1);
    }
    void push( LL S , LL V )
    {
        int index = S % 10007;
        for( int k = head[index]; k != -1; k = next[k] )
            if ( state[k] == S )
            {
                value[k] = max( value[k] , V );
                return;
            }
        state[size] = S , value[size] = V;
        next[size] = head[index] , head[index] = size++;
    }
}dp[2];

void init()
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        scanf("%d",&g[i][j]);
}

inline void decode( LL S , int m )
{
    for( int i = 0; i <= m; i++ ) code[i] = S & bit , S >>= inc;
}

inline LL encode( int m )
{
    LL ans = 0;
    int now = 1;
    Clean(vis,-1);
    vis[0] = 0;
    for( int i = m; i >= 0; i-- )
    {
        if ( -1 == vis[ code[i] ] ) vis[code[i]] = now++;
        code[i] = vis[ code[i] ];
        ans <<= inc;
        ans |= code[i];
    }
    return ans;
}

void DP( int x , int y , int k )
{
    decode( dp[pre].state[k] , m );
    int left = code[y-1] , up = code[y];
    code[y] = code[y-1] = 0;
    LL V = dp[pre].value[k];

    if ( x == n && y == m )
    {
        if ( ( !up && left ) || ( !left && up ) ) //單插頭
        {
            dp[cur].push( encode( m ) , V + g[x][y] );
        }
    }
    else if ( x == 1 && y == 1 )
    {
        if ( x < n ) code[y-1] = 1 , dp[cur].push( encode( m ) , V + g[1][1] ) , code[y-1] = 0;
        if ( y < m ) code[y] = 1 , dp[cur].push( encode( m ) , V + g[1][1] );
    }
    else if ( !left && !up ) //不取該格子,無插頭,則不產生插頭
    {
        dp[cur].push( encode(m) , V );
        if ( x < n && y < m )
        {
            code[y] = code[y-1] = bit;
            dp[cur].push( encode(m) , V + g[x][y] );
        }
    }
    else if ( !left || !up ) //取該格子,有一個插頭,還需要一個插頭
    {
        if ( x < n ) code[y-1] = left + up , dp[cur].push( encode(m) , V + g[x][y] );//下插頭
        code[y] = code[y-1] = 0;
        if ( y < m ) code[y] = left + up , dp[cur].push( encode(m) , V + g[x][y] );//右插頭
    }
    else if ( left != up ) //有兩個插頭且不成迴路,即聯通兩個非聯通路線 不能形成迴路
    {
        for( int i = 0; i <= m; i++ )
            if( code[i] == left ) code[i] = up;
        dp[cur].push( encode( m ) , V + g[x][y] );
    }
}

LL solve()
{
    cur = 0;
    dp[0].clear();
    dp[0].push( 1 , 0 );

    for( int i = 1; i <= n; i++ )
    {
        pre = cur , cur ^= 1;
        dp[cur].clear();
        for( int k = 0; k < ( dp[pre].size ); k++ )
        dp[cur].push( dp[pre].state[k]<<inc , dp[pre].value[k] );

        for( int j = 1; j <= m; j++ )
        {
            pre = cur , cur ^= 1;
            dp[cur].clear();
            for( int k = 0; k < dp[pre].size; k++ ) DP( i , j , k );
        }
    }
    for( int k = 0; k < dp[cur].size; k++ )
        if ( dp[cur].state[k] == 0 ) return dp[cur].value[k];
    return 0;
}

int main()
{
    int cas = 0;
    while( scanf("%d%d",&n,&m) == 2 )
    {
        init();
        printf("Case %d: %lld\n",++cas,solve());
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章