zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    static,匿名对象
    构造方法
    面向对象
    数组拷贝,可变参数,foreach
    毕业设计 之 七 参考文献综述
    毕业设计 之 六 网站搭建学习笔记
    毕业设计 之 五 PHP语法学习笔记
    毕业设计 之 四 英文资料翻译
    毕业设计 之 三 mooodle及bigbluebutton使用笔记(未完成)
    毕业设计 之 二 PHP集成环境(Dreamweaver)使用
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7967533.html
Copyright © 2011-2022 走看看