HDU 1964 Pipes(插头DP-回路最小代价)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1964

题意:给定一个n*m的格子,以及相邻格子之间的代价。求一个代价最小的回路。

思路:每次用最小的代价更新;每次转移时加上转移代价。

#include <iostream>

#include <cstdio>

#include <string.h>

#include <algorithm>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <map>





#define max(x,y) ((x)>(y)?(x):(y))

#define min(x,y) ((x)<(y)?(x):(y))

#define abs(x) ((x)>=0?(x):-(x))

#define i64 long long

#define u32 unsigned int

#define u64 unsigned long long

#define clr(x,y) memset(x,y,sizeof(x))

#define CLR(x) x.clear()

#define ph(x) push(x)

#define pb(x) push_back(x)

#define Len(x) x.length()

#define SZ(x) x.size()

#define PI acos(-1.0)

#define sqr(x) ((x)*(x))



#define FOR0(i,x) for(i=0;i<x;i++)

#define FOR1(i,x) for(i=1;i<=x;i++)

#define FOR(i,a,b) for(i=a;i<=b;i++)

#define FORL0(i,a) for(i=a;i>=0;i--)

#define FORL1(i,a) for(i=a;i>=1;i--)

#define FORL(i,a,b)for(i=a;i>=b;i--)





#define rush() int C; for(scanf("%d",&C);C--;)

#define Rush(n)  while(scanf("%d",&n)!=-1)

using namespace std;





void RD(int &x){scanf("%d",&x);}

void RD(i64 &x){scanf("%lld",&x);}

void RD(u32 &x){scanf("%u",&x);}

void RD(double &x){scanf("%lf",&x);}

void RD(int &x,int &y){scanf("%d%d",&x,&y);}

void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}

void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}

void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}

void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}

void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}

void RD(char &x){x=getchar();}

void RD(char *s){scanf("%s",s);}

void RD(string &s){cin>>s;}



void PR(int x) {printf("%d\n",x);}

void PR(i64 x) {printf("%lld\n",x);}

void PR(u32 x) {printf("%u\n",x);}

void PR(double x) {printf("%.4lf\n",x);}

void PR(char x) {printf("%c\n",x);}

void PR(char *x) {printf("%s\n",x);}

void PR(string x) {cout<<x<<endl;}





const int INF=1000000000;

const int HASHSIZE=100007;

const int N=1000005;



int n,m,code[15],cur,pre,sx,sy;

char s[100][100];

int ans;



struct node

{

    int e,next[N],head[HASHSIZE];

    i64 cnt[N],state[N];



    void init()

    {

        clr(head,-1);

        e=0;

    }



    void push(i64 s,i64 val)

    {

        int i,x=s%HASHSIZE;

        for(i=head[x];i!=-1;i=next[i])

        {

            if(state[i]==s)

            {

                cnt[i]=min(cnt[i],val);

                return;

            }

        }

        state[e]=s;

        cnt[e]=val;

        next[e]=head[x];

        head[x]=e++;

    }

};





node dp[2];



void decode(int code[],int m,i64 st)

{

    int i;

    FORL0(i,m) code[i]=st&7,st>>=3;

}



i64 encode(int code[],int m)

{

    i64 ans=0;

    int hash[15],i,cnt=1;

    clr(hash,-1);

    hash[0]=0;

    FOR0(i,m+1)

    {

        if(hash[code[i]]==-1) hash[code[i]]=cnt++;

        code[i]=hash[code[i]];

        ans=(ans<<3)|code[i];

    }

    return ans;

}





int get(int x1,int y1,int x2,int y2)

{

    if(x1==x2) return s[x1<<1][y1+y2]-'0';

    return s[x1+x2][y1<<1]-'0';

}





void update1(int i,int j)

{

    int x,y,k,t,q=j==m?m-1:m;

    i64 c;

    FOR0(k,dp[pre].e)

    {

        decode(code,m,dp[pre].state[k]);

        c=dp[pre].cnt[k];

        x=code[j-1];

        y=code[j];

        if(x&&x==y)

        {

            if(i==sx&&j==sy) ans=min(ans,c+get(i-1,j,i,j)+get(i,j-1,i,j));

        }

        else if(x&&y&&x!=y)

        {

            code[j-1]=code[j]=0;

            FOR0(t,m+1) if(code[t]==y) code[t]=x;

            dp[cur].push(encode(code,q),c+get(i-1,j,i,j)+get(i,j-1,i,j));

        }

        else if(x||y)

        {

            if(x) c+=get(i,j-1,i,j);

            else c+=get(i-1,j,i,j);

            if(i<n&&s[i+1][j])

            {

                code[j-1]=x+y;

                code[j]=0;

                dp[cur].push(encode(code,q),c);

            }

            if(j<m&&s[i][j+1])

            {

                code[j-1]=0;

                code[j]=x+y;

                dp[cur].push(encode(code,q),c);

            }

        }

        else if(x==0&&y==0)

        {

            if(i<n&&j<m&&s[i][j+1]&&s[i+1][j])

            {

                code[j-1]=code[j]=13;

                dp[cur].push(encode(code,q),c);

            }

        }

    }

}





int DP()

{

    ans=INF; pre=0,cur=1;

    dp[0].init(); dp[0].push(0,0);

    int i,j;

    FOR1(i,n) FOR1(j,m)

    {

        dp[cur].init();

        update1(i,j);

        pre^=1;

        cur^=1;

    }

    return ans;

}



int main()

{

    rush()

    {

        scanf("%d%d\n",&n,&m);

        int i;

        FOR1(i,n+n+1) gets(s[i]+1);

        sx=n; sy=m;

        PR(DP());

    }

    return 0;

}

  

你可能感兴趣的:(HDU)