HDU3414 Tour Route(竞赛图寻找哈密顿回路)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3414

题意:

  某个城市有N个景点,某一天来了一批游客想参观这些景点,他们的要求是这些景点都要去且每个景点仅去一次.特殊的是,对于任意两个景点,路都是单向的.即要么能从A景点到B景点,要么可以从B景点到A景点,不存在双向或者不连通的情况.让你找到一个回路,从某个景点出发,经过全部景点一次且仅一次,最后又能回到起点.

思路:

  很显然是让在竞赛图中寻找哈密顿回路,但是由于竞赛图一定存在哈密顿路径,但不一定存在哈密顿回路,所以需要枚举所有起点,构造一个哈密顿路径,然后判断起点和终点是否连通就可以了.

代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>

using namespace std;
typedef long long LL;
const int maxN = 1000;

inline void read(int&a){char c;while(!(((c=getchar())>=‘0‘)&&(c<=‘9‘)));a=c-‘0‘;while(((c=getchar())>=‘0‘)&&(c<=‘9‘))(a*=10)+=c-‘0‘;}

void Hamilton(int ans[maxN + 7], int map[maxN + 7][maxN + 7], int n, int st){
    int nxt[maxN + 7];
    memset(nxt, -1, sizeof(nxt));
    int head = st;
    for(int i = 1; i <= n; i++){
        if(i == st)continue;
        if(map[i][head]){
            nxt[i] = head;
            head = i;
        }else{
            int pre = head, pos = nxt[head];
            while(pos != -1 && !map[i][pos]){
                pre = pos;
                pos = nxt[pre];
            }
            nxt[pre] = i;
            nxt[i] = pos;
        }
    }
    int cnt = 0;
    for(int i = head; i != -1; i = nxt[i])
        ans[++cnt] = i;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int N;
    while(~scanf("%d", &N) && N){
        int map[maxN + 7][maxN + 7] = {0};
        for(int i = 1; i <= N; i++){
            for(int j = 1; j <= N; j++){
                int u;
                read(u);
                map[i][j] = u;
            }
        }
        if(N == 1){printf("1\n");continue;}
        int ans[maxN + 7] = {0}, i;
        for(i = 1; i<= N; i++){
            Hamilton(ans, map, N, i);
            if(map[ans[N]][ans[1]]){
                for(int j = 1; j <= N; j++){
                    printf(j == 1 ? "%d":" %d", ans[j]);
                }
                break;
            }
        }
        if(i > N)printf("-1");
        printf("\n");
    }
    return 0;
}
时间: 2024-08-09 22:01:38

HDU3414 Tour Route(竞赛图寻找哈密顿回路)的相关文章

hdu 3414 Tour Route(哈密顿回路判定)

题意:给定一个邻接矩阵得到有向图,判断是否存在哈密顿回路,若存在,输出路径,否则输出-1: 思路:将每个点作为head遍历一次求哈密顿通路,看是否存在哈密顿回路:一个点的时候需要特判: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m; int mm[1005][1005]; int nxt[500010]; int cnt[500010],ans; v

基于回溯法寻找哈密顿回路

回溯法是一种选优搜索法,又称为试探法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”. 在包含问题的所有解的解空间树中,按照深度优先搜索的策略,从根结点出发深度探索解空间树.当探索到某一结点时,要先判断该结点是否包含问题的解,如果包含,就从该结点出发继续探索下去,如果该结点不包含问题的解,则逐层向其祖先结点回溯(其实回溯法就是对隐式图的深度优先搜索算法). 若用

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?       

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123

图论500题

=============================以下是最小生成树+并查集====================================== [HDU] 1213   How Many Tables   基础并查集★ 1272   小希的迷宫   基础并查集★ 1325&&poj1308  Is It A Tree?   基础并查集★ 1856   More is better   基础并查集★ 1102   Constructing Roads  基础最小生成树★ 12

图论题集收藏

 ===================下面是最小生成树+并查集====================================== [HDU] 1198   Farm Irrigation   并查集★(好题) 1598   find the most comfortable road 枚举+最小生成树★★ 1811   Rank of Tetris   并查集+拓扑排序★★ 3926   Hand in Hand   同构图★ 3938   Portal     离线+并查集★★