hduTour Route【竞赛图的哈密顿回路】

Problem Description
The city is so crowded that the mayor can't bear any longer. He issued an order to change all the roads into one-way street. The news is terrible for Jack, who is the director of a tourism company, because he has to change the travel route. All tourists want to set out from one scenic spot, then go to every scenic spots once and only once and finally return to the starting spot. They don’t care about which spot to start from, but they won’t go back to the starting spot before they have visited all other spots. Fortunately, the roads in the city have been perfectly built and any two scenic spots have been connected by ONE road directly. Jack gives the map of the city to you, and your task is to arrange a new travel route around the city which can satisfy the tourists.
 

Input
Input consists of multiple test cases and ends with a line of “0”.
For each test case:
The first line contains a single integer n (0<n<=1000), representing the number of city scenic spots. Scenic spots are numbered form 1 to n.
Then n lines follows, and each line consists of n integers. These n lines make a matrix. If the element in the ith row and the jth column is 1(i≠j), it means that the direction of the road between spot i and spot j is from spot i to spot j. If that element is 0, it means that the road’s direction is from spot j to spot i. The numbers in the main diagonal of the matrix are all 0. (i and j start from 1)
 

Output
For each test case, print all the spots No. according to the traveling order of the route in one line. If multiple routes exist, just print one of them. If no such route exists, print a “-1” instead. Because the starting spot is the same as the ending spot, so you don’t need to print the ending spot.

This problem needs special judge.
 

Sample Input
  
  
5 0 0 1 1 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 2 0 1 0 0 0
 

Sample Output
  
  
1 3 4 5 2 -1
 

Source
 

本以为竞赛图下的哈密顿回路没什么特别的,结果做了将近一个下午一直到现在(手动再见

先说何为竞赛图,图中所有点两两之间存在有向边。而且竞赛图一定是存在哈密顿路径的。所以找完一条哈密顿路径还要判断尾->首元素是否有路径。

为了仿照下一个题(竞赛图下的哈密顿路径)代码稍有修改

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #include<algorithm>
    using namespace std;
    const int maxn=1100;
    int N;
    int g[maxn][maxn];
    int nex[maxn];
    bool expend(int st)
    {
        for(int i=0;i<N;i++)nex[i]=-1;
        int head=st,tail=head;
        for(int i=0;i<N;i++)
        {
            if(i==st)continue;
            if(g[i][head])nex[i]=head,head=i;//这里的意思是如果发现另一个点到这个头结点有路径,那么那个点就变成头结点了,所以说,网上的代码是有潜在bug的
            else
            {
                int x=head,y=nex[head];
                while(y!=-1&&g[y][i])
                {
                    x=y;
                    y=nex[y];
                }
                nex[x]=i;
                nex[i]=y;
                if(y==-1)tail=i;
            }
        }
        if(g[tail][head])
        {
            nex[tail]=head;
            return true;
        }
        return false;
    }
    int solve()
    {
        for(int i=0;i<N;i++)
            if(expend(i))return i+1;
        return 0;
    }
    int main()
    {
       // freopen("cin.txt","r",stdin);
        while(scanf("%d",&N)!=EOF,N)
        {
            for(int i=0;i<N;i++)
                for(int j=0;j<N;j++)
                    scanf("%d",&g[i][j]);
            if(N==1){printf("1\n");continue;}
            int head=solve();
            if(N==2||!head)printf("-1\n");
            else
                for(int i=0,j=head-1;i<N;i++,j=nex[j])
                    printf("%d%c",j+1,i==N-1?'\n':' ');
        }
        return 0;
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值