hdu 3414----竞赛图中找寻是否存在哈密顿回路

来源:互联网 发布:京颐集团 知乎 编辑:程序博客网 时间:2024/05/05 06:36


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
50 0 1 1 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 1 0 1 1 0 020 10 00

Sample Output
1 3 4 5 2-1


题意:
  某个城市有N个景点,某一天来了一批游客想参观这些景点,他们的要求是这些景点都要去且每个景点仅去一次.特殊的是,对于任意两个景点,路都是单向的.即要么能从A景点到B景点,要么可以从B景点到A景点,不存在双向或者不连通的情况.让你找到一个回路,从某个景点出发,经过全部景点一次且仅一次,最后又能回到起点.
思路:
  很显然是让在竞赛图中寻找哈密顿回路,但是由于竞赛图一定存在哈密顿路径,但不一定存在哈密顿回路,所以需要枚举所有起点,构造一个哈密顿路径,然后判断起点和终点是否连通就可以了.


哈密顿图
通过图中的顶点一次且仅一次的通路(回路)称为哈密顿通路(回路)
具有哈密顿回路的图成为哈密顿图
平凡图也为哈密顿图
每对顶点之间只有一条边相连的有向图称为竞赛图。


n(n>=2)阶竞赛图一定存在哈密顿通路

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<vector>#include<set>#include<algorithm>#include<sstream>#define LL long long#define inf 0x3f3f3f3fusing namespace std;const int MAXN=1010;bool mapx[MAXN][MAXN];int path[MAXN];int nextx[MAXN];int n;bool hamidun(){    for(int i=1;i<=n;i++)//寻找起点为i的哈密顿回路,若找到直接退出    {        memset(nextx,-1,sizeof(nextx));        int head=i;        for(int j=1;j<=n;j++)        {            if(i==j)                continue;            if(mapx[j][head]==1)            {                nextx[j]=head;                head=j;            }//不断向后添加路径            else            {                int pre=head,pos=nextx[head];                while(pos!=-1&&mapx[pos][j]==1)                {                    pre=pos;                    pos=nextx[pre];                }//找到后,路径为pre→j→pos                nextx[pre]=j;                nextx[j]=pos;            }        }        int k=1;        for(int j=head;j!=-1;j=nextx[j])            path[k++]=j;        if(mapx[path[k-1]][head]==1)            return true;    }    return false;}int main(){    while(cin>>n)    {        if(n==0)            return 0;        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)                scanf("%d",&mapx[i][j]);        if(n==1)//特殊判断        {            cout<<1<<endl;            continue;        }        bool t=hamidun();        if(t==0)            cout<<-1<<endl;        else//输出哈密顿路径        {            for(int i=1;i<=n-1;i++)                cout<<path[i]<<" ";            cout<<path[n]<<endl;        }    }    return 0;}



0 0