• HihoCoder1087Hamiltonian Cycle(DP状态压缩)


    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us how many different Hamiltonian Cycles are there in this graph?

    A Hamiltonian Cycle is a cycle that starts from some vertex, visits each vertex (except for the start vertex) exactly once, and finally ends at the start vertex.

    Two Hamiltonian Cycles C1, C2 are different if and only if there exists some vertex i that, the next vertex of vertex i in C1 is different from the next vertex of vertex i in C2.

    输入

    The first line contains two integers n and m. 2 <= n <= 12, 1 <= m <= 200.

    Then follows m line. Each line contains two different integers a and b, indicating there is an directed edge from vertex a to vertex b.

    输出

    Output an integer in a single line -- the number of different Hamiltonian Cycles in this graph.

    提示

    额外的样例:

    样例输入 样例输出
    3 3
    1 2               
    2 1              
    1 3
    0



    样例输入
    4 7
    1 2
    2 3
    3 4
    4 1
    1 3
    4 2
    2 1
    样例输出
    2

    搜索大概也可以搞定。

    • 求哈密顿环的数目
    • 既然是环,且每个点都经过,我们假定一个起点,得到的结果是一样的,我的代码假定的是1为起点。
    • 这题有重边,但是必须两个点之间重边只看成一条边才能AC
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<cstring>
    using namespace std;
    int Mp[20][20];
    int dp[1<<12][20];//vis,now
    int main()
    {
        int n,m,x,y,i,k,p,ans=0;
        scanf("%d%d",&n,&m);
        for(i=1;i<=m;i++){
            scanf("%d%d",&x,&y);
            Mp[x][y]=1;
        }
        for(i=1;i<=n;i++) dp[1][1]=1;
        for(i=1;i<(1<<n);i++)
        {
             for(k=1;k<=n;k++)//now
             {
                if(!(i&(1<<(k-1)))) continue;
                for(p=1;p<=n;p++)//pre
                {  
                   if(!(i&(1<<(p-1)))||k==p) continue;
                   dp[i][k]=dp[i][k]+dp[i^(1<<(k-1))][p]*Mp[p][k];
                }
             }
        }
        for(i=1;i<=n;i++) ans+=dp[(1<<n)-1][i]*Mp[i][1];
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    enmo_day_07
    enmo_day_04
    enmo_day_05
    数据仓库的模型设计
    Lucene 概念,定义应用场景
    enum 枚举的简单应用
    单例模式&synchronized
    Spark的 DAGschedule & task schedule 区别以及相互联系
    Spark的stage & job & task 到底是什么 ,以及划分原理
    Java基本数据类型&引用类型总结
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7967533.html
Copyright © 2020-2023  润新知