John's trip (poj-1041,欧拉回路,字典序最小路径)

这篇博客讨论了如何帮助Johnny找到城镇中字典序最小的欧拉回路。Johnny希望在拜访所有朋友后回到起点,每个街道只经过一次。文章解释了如何确定是否存在这样的路径,并介绍了使用深搜算法来找到最小的路径序列。如果不存在欧拉回路,程序会输出特定消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible. Very soon he realized that the best way to do it was to travel through each street of town only once. Naturally, he wanted to finish his trip at the same place he started, at his parents' house. 

The streets in Johnny's town were named by integer numbers from 1 to n, n < 1995. The junctions were independently named by integer numbers from 1 to m, m <= 44. No junction connects more than 44 streets. All junctions in the town had different numbers. Each street was connecting exactly two junctions. No two streets in the town had the same number. He immediately started to plan his round trip. If there was more than one such round trip, he would have chosen the one which, when written down as a sequence of street numbers is lexicographically the smallest. But Johnny was not able to find even one such round trip. 

Help Johnny and write a program which finds the desired shortest round trip. If the round trip does not exist the program should write a message. Assume that Johnny lives at the junction ending the street appears first in the input with smaller number. All streets in the town are two way. There exists a way from each street to another street in the town. The streets in the town are very narrow and there is no possibility to turn back the car once he is in the street 

Input

Input file consists of several blocks. Each block describes one town. Each line in the block contains three integers x; y; z, where x > 0 and y > 0 are the numbers of junctions which are connected by the street number z. The end of the block is marked by the line containing x = y = 0. At the end of the input file there is an empty block, x = y = 0.

Output

Output one line of each block contains the sequence of street numbers (single members of the sequence are separated by space) describing Johnny's round trip. If the round trip cannot be found the corresponding output block contains the message "Round trip does not exist."

Sample Input

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

Sample Output

1 2 3 5 4 6 
Round trip does not exist.

题意:

给若干条路

由x,y,z构成,  双向路x<->y  编号为z(同样的两个路口,可以存在多条路,只要编号不一样,就是不同的路)

求欧拉回路

如果没有欧拉回路输出:    Round trip does not exist.

解析:

我们先判断是否为欧拉回路,这个图已经是连通图,我们只要判断每个点度数是否为偶数

再用深搜求字典序最小的路径

我们用gra[M][N] 存路径,gra[a][w]=b表示a<->b   路径编号为w,深搜时,按路径编号从小到大遍历,就能找到最小序列

用一个栈记录路径,每次只有回溯到当前点,就是说当前点的后继都已经搜过了的时候,才把当前点入栈,这样一来倒着输出,就能得到一个欧拉回路,而且是最小升序。

ac:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,l,r) for(int i=l;i<r;i++)
#define mem(gv) memset(gv,0,sizeof(gv))
#define ll long long
#define N 2005
#define M 55
using namespace std;

int gra[M][N],ind[M];//gra[i][j]=k表示点x经过边j到达点k,ind[i],i点度数
int ans[N],top;//ans记录路径,cnt
int vis[N];//标记是否被访问过
int maxn;  //最大街道编号

void dfs(int t)
{
    rep(i,1,maxn+1)//按路径编号从小到大遍历
    {
        if(gra[t][i]&&vis[i]==0)
        {
            vis[i]=1;
            dfs(gra[t][i]);
            ans[top++]=i;//,每次只有回溯到当前点,就是说当前点的后继都已经搜过了的时候,才把当前点入栈
        }
    }
}

int main()
{
    int st;
    int u,v,w,maxx,flag;
    while(scanf("%d%d",&u,&v))
    {
        if(u==0&&v==0)
            break;
        mem(gra);mem(ind);mem(vis);
        maxn=0;
        maxx=0; //最大路口编号

        while(u!=0&&v!=0)
        {
            st=min(u,v);
            scanf("%d",&w);
            gra[u][w]=v;
            gra[v][w]=u;
            ind[u]++;
            ind[v]++;
            maxn=max(maxn,w);
            maxx=max(maxx,max(u,v));
            scanf("%d%d",&u,&v);
        }

        flag=0;            //判断是否是欧拉回路
        rep(i,1,maxx+1)
        {
            if(ind[i]%2==1)
            {
                printf("Round trip does not exist.\n");
                flag=1;
                break;
            }
        }
        if(flag)
            continue;

        top=0;              //用一个栈记录路径
        dfs(st);
        for(int i=top-1;i>=1;i--)//逆序输出
            printf("%d ",ans[i]);
        printf("%d\n",ans[0]);
    }
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值