poj_1041 John's trip

John's trip

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 5455

Accepted: 1774

Special Judge

题目链:http://poj.org/problem?id=1041

 

 

 

 

Description

Little Johnny has got a new car. He decided to drivearound 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 startedthinking how to make his trip as short as possible. Very soon he realized thatthe 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 hisparents' 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 townhad different numbers. Each street was connecting exactly two junctions. No twostreets in the town had the same number. He immediately started to plan hisround trip. If there was more than one such round trip, he would have chosenthe one which, when written down as a sequence of street numbers islexicographically the smallest. But Johnny was not able to find even one suchround trip.

Help Johnny and write a program which finds the desired shortest round trip. Ifthe round trip does not exist the program should write a message. Assume thatJohnny lives at the junction ending the street appears first in the input withsmaller number. All streets in the town are two way. There exists a way fromeach street to another street in the town. The streets in the town are verynarrow and there is no possibility to turn back the car once he is in thestreet

Input

Input file consists of several blocks. Each blockdescribes 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 bythe 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 ofstreet numbers (single members of the sequence are separated by space)describing Johnny's round trip. If the round trip cannot be found thecorresponding output block contains the message "Round trip does notexist."

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.

Source

Central Europe 1995

题意:

       x,y,z表示起点,终点和这条边的编号,从图的起点出发,问是否能访问完所有边,然后回到起点,访问的相同边根据字典序列来访问。

解题思路:

         这个题目可以用欧拉回路来决解,首先根据输入建立图,保存图中每条边的编号,起点,终点,记录边数和点数,用欧拉回路来判断该图是否存在欧拉回路,如果存在,则递归遍历每一条边,直到所有边都遍历完成。遍历的起点为上一条边的终点。

代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#define MAX 50
using namespace std;

struct edge
{//存边
    int start,end;
};
edge e[2000];//边数
int g[MAX][MAX];
int indegree[MAX];
int outdegree[MAX];
int temp[MAX];
int visited[2000];//记录某节点是否被访问过
int t;
int v,es;//记录点数和边数

//从起点s开始遍历每一条边
void euler(int s)
{
    for(int i=1;i<=es;i++)
    {
        //该条边没有被访问过,并且该边的起点或者终点是要访问的点s
        if(visited[i]==0 && (e[i].start==s||e[i].end==s))
        {
            visited[i]=1;//访问该条边
            euler(e[i].start+e[i].end-s);//从起点访问到终点,再以终点为起点开始访问下一条边
            temp[t]=i;
            t++;
        }
    }
}

int main()
{
    int x,y,z;
    int i,j;
    int flag=0;
    while(true)
    {
        int start;
        memset(g,0,sizeof(g));
        memset(visited,0,sizeof(visited));
        memset(indegree,0,sizeof(indegree));
        memset(outdegree,0,sizeof(outdegree));
        v=0;
        es=0;
        t=0;
        while(true)
        {
            scanf("%d%d",&x,&y);
            if(x==0 && y==0)
            {
                flag++;
                if(flag==2)
                    return 0;
                break;
            }
            //求出起点,x,y中的最小值
            start=x;
            if(start>y)
                start=y;
            flag=0;
            scanf("%d",&z);
            g[x][y]=1;
            g[y][x]=1;
            e[z].start=x;//图已经的连通图,不用再判断是否连通
            e[z].end=y;
            //计算边数和点数
            if(es<z)
                es=z;
            if(v<x)
                v=x;
            if(v<y)
                v=y;
			//记录出度和入度,用来判断欧拉路
            indegree[x]++;
            outdegree[y]++;
        }

        //判断是否是欧拉回路
        for(i=1;i<=v;i++)
        {
            for(j=1;j<=v;j++)
            {
                if(g[i][j]==1 && (outdegree[j]+indegree[j])%2!=0)
                {
                    printf("Round trip does not exist.\n");
                    break;
                }
            }
            if(j<=v)
                break;
        }
        if(i<=v)
            continue;
        euler(start);
        for(i=es-1;i>=1;i--)
        {
            printf("%d ",temp[i]);
        }
        printf("%d\n",temp[0]);
    }

    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值