汉密尔顿图与欧拉图

  • Post author:
  • Post category:其他

汉密尔顿图:

定义:哈密顿通路(回路)与哈密顿图 (Hamilton图) 通过图G的每个结点一次,且仅一次的通路(回路),就是哈密顿通路(回路)。存在哈密顿回路的图就是哈密顿图,引申出来一个TSP问题求解的是给定一个有向图或者无向图求解这个图中的最小汉密尔顿图

 

Granny’s Bike

发布时间: 2017年6月19日 00:27   时间限制: 1000ms   内存限制: 128M

描述

Most days Granny rides her bike around town to do errands, visit, have a cup of coffee, and so on. She enjoys riding her bike and wants to avoid passing the same place twice to add to the interest of the ride. So, each day she draws a map of the places to be visited, with lines connecting those near each other, and sees if she can visit them all and return home without passing a place more than once. Some days she finds she can do this and other days she finds she can’t. For example, for the map on the left, Granny can visit every place and return home without passing any place twice, but she can’t do it for the map on the right.

[图片]

She turns to you to write a program to help her.

输入

 

There will be multiple test cases for this problem. Each test case will have input on multiple lines. The first line will contain the integer n (< 10) noting the number of places Granny wants to visit that day. These will be numbered 1 through n and Granny’s house will be numbered 0. The next n lines will be a list of those places near each spot. The first line will be a list of places with a direct route from place 1. The second line will be a list of places with a direct route from place 2, and so on. You may assume that if place i has a direct route to place j, then there is a direct route the other direction also. A line containing 0 will follow the last test case.

 

输出

 

For each test case, print one line of the form:Case m: Granny can make the circuit.
Or Case m: Granny can not make the circuit as appropriate. Here, m is the number of the test case, starting at 1.

 

样例输入1

5
0 2 5
0 1 3
2 4
0 3 5
1 4
4
0 2 3 4
1 3
1 2
0 1
0

样例输出1

Case 1: Granny can make the circuit.
Case 2: Granny can not make the circuit.

题意:从0点出发,输入数据n代表要去旅行的城市数目,之后n行表示第i行即第i个城市与哪几个城市相连,求能否每个城市都旅行一次再返回起始点0点

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
typedef long long LL;
int tu[25][25];
int vis[25];
int n;
int sum;
int flag;
bool dfs(int k)
{
    if(sum==n+1&&k==0)
    {
        return true;
    }

    for(int i=0; i<=n; i++)
    {
        if(!vis[i]&&tu[k][i])
        {
            sum++;
            vis[i]=1;
            if(dfs(i))
            {
                return true;
            }
            sum--;
            vis[i]=0;
        }
    }
    return false;
}
int main()
{
    char a[25];
    int hh;
    int len;
    int t=0;
    while(scanf("%d",&n)&&n)
    {
        flag=0;
        sum=0;
        memset(tu,0,sizeof(tu));
        memset(vis,0,sizeof(vis));
        getchar();
        for(int i=1; i<=n; i++)
        {
            memset(a,0,sizeof(a));
            gets(a);
            len=strlen(a);
            for(int j=0; j<len; j+=2)
            {
                hh=a[j]-'0';
                tu[i][hh]=tu[hh][i]=1;
            }
        }
        if(dfs(0))
        {
            cout<<"Case "<<++t<<":"<<" Granny can make the circuit."<<endl;
        }
        else
        {
            cout<<"Case "<<++t<<":"<<" Granny can not make the circuit."<<endl;
        }
    }
    return 0;
}

 

 

 


版权声明:本文为bengshakalakaka原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。