tongji 30029 插头dp

本文详细介绍了Hnoi2004postman问题的求解思路,包括三种不同情况下的解决方案及代码实现,特别关注了保持n>m的重要性以提升速度,并使用高精度计算以避免数据溢出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.tongji.edu.cn/problem?pid=30029

Hnoi2004 postman 

题意:从左上角出发,最后回到左上角,要求经过所有点至少一次,求步数最小的方案数。(n<=10,m<=20)

分析:有三种情况。
1.n,m至少有一个为1,则答案为1
2.n,m至少有一个偶数,则答案为哈密顿回路条数*2,
  此时正好经过所有点一次除(1,1)外
3.【数据里并没有出现这种情况】
  n, m均为大于1的奇数,这时不存在哈密顿回路,
  则最短步数应为从(1,1)出发,终于(1,3),(2,2),(3,1)。
  答案为到上面任一种的条数*3
参考 http://blog.youkuaiyun.com/fp_hzq/article/details/7523056

还有,要保持n>m,因为当列数12已经是极限了,是不能达到20的,所以应该交换。
保证n>m(即m<=10)后速度得到了极大提升。

最后用上高精,因为10 20答案超了long long

给出几组数据

4 4
12
6 6
2144
9 16
619313040592944136
10 20  
177029033285148340652006844

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#define MS(x, y) memset(x, y, sizeof(x))
#define FOR(i, x, y) for(int i=x; i<=y; i++)
#define rFOR(i, x, y) for(int i=x; i>=y; i--)
using namespace std;
const int maxn = 100;
struct bigint
{   int s[maxn];
    bigint() {MS(s, 0);}
    bigint(int num) {*this = num;}
    bigint(const char* num){*this = num;}

    bigint operator = (const char* num) //字符串赋值
    {
        MS(s, 0);
        if(num[0] == '-')
        {
            num = num + 1;
            s[0] = -1;
        }
        else s[0] = 1;
        while(num[0] == '0') num = num + 1;
        s[0] = s[0] * strlen(num);
        int len = abs(s[0]);
        FOR(i, 1, len) s[i] = num[len - i] - 48;
        return *this;
    }
    bigint operator = (int num)         //整数赋值
    {
        char s[maxn];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }

    string str() const                      //返回字符串
    {
        string res = "";
        FOR(i, 1, abs(s[0])) res = (char)(s[i] + 48) + res;
        if(res == "") return res = "0";
        if(s[0] < 0) res = '-' + res;
        return res;
    }
    //----------以上是构造部分,以下是运算部分----------//
    bool operator < (const bigint& b) const
    {
        if(s[0] != b.s[0]) return s[0] < b.s[0];
        int len = abs(s[0]);
        rFOR(i, len, 1)
        if(s[i] != b.s[i])
            return (s[i] < b.s[i]) ^ (s[0] < 0);
        return false;
    }
    bool operator > (const bigint& b) const{return b < *this;}
    bool operator <= (const bigint& b) const{return !(b < *this);}
    bool operator >= (const bigint& b) const{return !(*this < b);}
    bool operator != (const bigint& b) const{return b < *this || *this < b;}
    bool operator == (const bigint& b) const{return !(b < *this) && !(*this < b);}

    friend bigint abs(bigint b)
    {
        b.s[0] = abs(b.s[0]);
        return b;
    }
    friend bigint _add(const bigint& a, const bigint& b)
    {
        bigint c;
        c.s[0] = max(a.s[0], b.s[0]);
        FOR(i, 1, c.s[0]) c.s[i] = a.s[i] + b.s[i];
        FOR(i, 1, c.s[0])
        if(c.s[i] >= 10)
        {
            c.s[i+1]++;
            c.s[i]-=10;
        }
        if(c.s[c.s[0]+1]) ++c.s[0];
        return c;
    }
    friend bigint _sub(const bigint& a, const bigint& b)
    {
        bigint c;
        c.s[0] = a.s[0];
        FOR(i, 1, c.s[0]) c.s[i] = a.s[i] - b.s[i];
        FOR(i, 1, c.s[0])
        if(c.s[i] < 0)
        {
            c.s[i+1]--;
            c.s[i] += 10;
        }
        while(!c.s[c.s[0]] && c.s[0]) --c.s[0];
        return c;
    }
    bigint operator + (const bigint& b) const
    {
        if(s[0] >= 0 && b.s[0] >= 0) return _add(*this, b);
        if(b.s[0] < 0) return *this - abs(b);
        if(s[0] < 0) return b - abs(*this);
    }
    bigint operator - (const bigint& b) const
    {
        if(s[0] >= 0 && b.s[0] >= 0)
        {
            bigint c;
            if(*this >= b) return _sub(*this, b);
            c = _sub(b, *this);
            c.s[0] = -c.s[0];
            return c;
        }
        if(b.s[0] < 0) return *this + abs(b);
        if(s[0] < 0)
        {
            bigint c;
            c = abs(*this) + b;
            c.s[0] = -c.s[0];
            return c;
        }
    }
    bigint operator * (const bigint& b) const
    {
        bigint c;
        c.s[0] = abs(s[0]) + abs(b.s[0]);
        FOR(i, 1, abs(s[0]))
        FOR(j, 1, abs(b.s[0]))
            c.s[i + j - 1] += s[i] * b.s[j];
        FOR(i, 1, c.s[0])
        {
            c.s[i+1] += c.s[i] / 10;
            c.s[i] %= 10;
        }
        while(!c.s[c.s[0]] && c.s[0]) --c.s[0];
        if((s[0] > 0) != (b.s[0] > 0)) c.s[0] = -c.s[0];
        return c;
    }
    bigint operator / (const bigint& _b) const
    {
        bigint c, t;
        bigint b = abs(_b);
        c.s[0] = abs(s[0]);
        rFOR(i, c.s[0], 1)
        {
            rFOR(j, t.s[0], 1) t.s[j + 1] = t.s[j];
            t.s[1] = s[i];
            ++t.s[0];

            while(t >= b)
            {
                ++c.s[i];
                t -= b;
            }
        }
        while(!c.s[c.s[0]] && c.s[0]) --c.s[0];
        if((s[0] > 0) != (b.s[0] > 0)) c.s[0] = -c.s[0];
        return c;
    }
    bigint operator % (const bigint& _b) const
    {
        bigint c, t;
        bigint b = abs(_b);
        rFOR(i, abs(s[0]), 1)
        {
            rFOR(j, t.s[0], 1) t.s[j + 1] = t.s[j];
            t.s[1] = s[i];
            ++t.s[0];

            while(t >= b) t -= b;
        }
        if((s[0] < 0)) t.s[0] = -t.s[0];
        return t;
    }

    bigint operator += (const bigint& b) {*this = *this + b;return *this;}
    bigint operator -= (const bigint& b) {*this = *this - b;return *this;}
    bigint operator *= (const bigint& b) {*this = *this * b;return *this;}
    bigint operator /= (const bigint& b) {*this = *this / b;return *this;}
    bigint operator %= (const bigint& b) {*this = *this % b;return *this;}

    friend bigint operator + (int& a, const bigint& b){return (bigint)a + b;}
    friend bigint operator - (int& a, const bigint& b){return (bigint)a - b;}
    friend bigint operator * (int& a, const bigint& b){return (bigint)a * b;}
    friend bigint operator / (int& a, const bigint& b){return (bigint)a / b;}
    friend bigint operator % (int& a, const bigint& b){return (bigint)a % b;}

    friend bigint operator <  (int& a, const bigint& b){return (bigint)a < b;}
    friend bigint operator <= (int& a, const bigint& b){return (bigint)a <=b;}
    friend bigint operator >  (int& a, const bigint& b){return (bigint)a > b;}
    friend bigint operator >= (int& a, const bigint& b){return (bigint)a >=b;}
    friend bigint operator == (int& a, const bigint& b){return (bigint)a ==b;}
    friend bigint operator != (int& a, const bigint& b){return (bigint)a !=b;}
};
istream& operator >> (istream &in, bigint& x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}
ostream& operator << (ostream &out, const bigint& x)
{
    out << x.str();
    return out;
}
typedef bigint LL;
const int N=22;
const int HASH=15111;
const int STATE=20000;

LL ans;
int n,m;
int map[N][N];
int jz[N];
int ex,ey;
struct HASHMAP
{
    int head[HASH],next[STATE],size;
    int state[STATE];
    LL f[STATE];
    void init()
    {
        size=0;
        memset(head,-1,sizeof(head));
    }
    void push(int st,LL ans)
    {
        int i;
        int h=st%HASH;
        for(i=head[h];i!=-1;i=next[i])
          if(state[i]==st)
          {
              f[i]+=ans;
              return;
          }
        state[size]=st;
        f[size]=ans;
        next[size]=head[h];
        head[h]=size++;
    }
}hm[2];

void shift(int cur)
{
    for(int i=0;i<hm[cur].size;i++)
     hm[cur].state[i]<<=2;
}
inline int cp(int p,int i)
{
    return p&(~(3<<jz[i]));
}
inline int cp(int p,int i,int j)
{
    return p&(~(3<<jz[i]))&(~(3<<jz[j]));
}
inline int cp(int p,int i,int j,int k)
{
    return p&(~(3<<jz[i]))&(~(3<<jz[j]))&(~(3<<jz[k]));
}
inline int getp(int p,int i)
{
    return 3&(p>>jz[i]);
}
inline int pp(int i,int k)
{   return k<<jz[i];
}
inline int lp(int p,int j)
{   for (int it=j-2,cnt=1;;it--)
        {   int pi=getp(p,it);
            if (pi==1) cnt--;
            else if (pi==2) cnt++;
            if (cnt==0) return it;
        }
}
inline int rp(int p,int j)
{   for (int it=j+1,cnt=1;;it++)
        {   int pi=getp(p,it);
            if (pi==2) cnt--;
            else if (pi==1) cnt++;
            if (cnt==0) return it;
        }
}
void dp(int i,int j,int cur)
{

    for(int k=0;k<hm[cur].size;k++)
        {int s=hm[cur].state[k];
         LL data=hm[cur].f[k];
         int left=getp(s,j);
         int up=getp(s,j+1);
         if (left==0&&up==0)
            {if (map[i][j+1]&&map[i+1][j])
                {
                    hm[cur^1].push(s|pp(j,1)|pp(j+1,2),data);
                }
             continue;
            }
         if (left==0||up==0)
            {   int w=left+up;
                int tmp=cp(s,j,j+1);
                if (map[i][j+1]) hm[cur^1].push(tmp|pp(j+1,w),data);
                if (map[i+1][j]) hm[cur^1].push(tmp|pp(j,w),data);
                continue;
            }
         if (left==up)
            {   int it=(left==1)?rp(s,j+1):lp(s,j+1);
                hm[cur^1].push(cp(s,j,j+1,it)|pp(it,left),data);
                continue;
            }
         if (left==1&&up==2)
            {   if (i==ex&&j==ey) ans+=data;
                continue;
            }
        if (left==2&&up==1)
            {   hm[cur^1].push(cp(s,j,j+1),data);
                continue;
            }
        }


}

void solve1()
{
    int cur=0;
    ans=0;
    hm[cur].init();
    hm[cur].push(0,1);
    for(int i=0;i<n;i++)
        {for(int j=0;j<m;j++)
            if (map[i][j])
            {
             hm[cur^1].init();
             dp(i,j,cur);
             cur^=1;
            }

         shift(cur);

        }
    cout<<ans*2<<endl;
}
void solve2()
{
    int cur=0;
    ans=0;
    hm[cur].init();
    hm[cur].push(0,1);
    for(int i=0;i<n;i++)
        {for(int j=0;j<m;j++)
            if (map[i][j])
            {
             hm[cur^1].init();
             dp(i,j,cur);
             cur^=1;
            }

         shift(cur);

        }
    cout<<ans*3<<endl;
}
void prepared_jinzhi()
{
    jz[0]=0;
    for (int i=1;i<=m;i++)
        jz[i]=i<<1;
}
void init1()
{   if (n<m) {int swap=n;n=m;m=swap;}
    memset(map,0,sizeof(map));
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            map[i][j]=1;
    ex=n-1; ey=m-1;
}
void init2()
{   if (n<m) {int swap=n;n=m;m=swap;}
    memset(map,0,sizeof(map));
    n+=2; m+=4;
    for (int i=2;i<n;i++)
        for (int j=2;j<m-2;j++)
            map[i][j]=1;
    for (int i=0;i<m;i++)
        map[0][i]=1;
    map[1][0]=map[1][m-1]=1;
    map[2][0]=map[2][1]=map[2][m-2]=map[2][m-1]=1;
    ex=n-1; ey=m-3;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {   if (n==1||m==1){printf("%d\n",1); continue;}
        if (n*m%2==1)
            {init2();
             prepared_jinzhi();
             solve2();
            }
        else {init1();
             prepared_jinzhi();
             solve1();
             }
    }
    return 0;
}






 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值