poj1041 John's trip(输出字典序最小的欧拉路径)

John’s trip

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.

题意:

从原点出发经过每条边一次,最后回到自己原点的地方。输出最短路线,且街道的字典序最小。

分析:

这题主要问题是如何处理街道的序号字典序最小。
用g(i,j)=k表示节点i可以通过街道j到达街道k
dfs的过程中从前开始遍历,递归最先找到的没有标记的 j ,这样就能保证字典序最小

建议看代码

code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
#include<algorithm>
#define lowbit(x) (x&(-(x)))
#define coff ios::sync_with_stdio(0)
const int inf=0x3f3f3f3f;
const int inn=-(1<<30);
using namespace std;
const int N=50;//点不超过50个
const int M=2005;//边不超过2000
int g[N][M];//g[i][j]=k 表示点i经过路径j到达点k
int mark[M];
int d[N];
int n,m;//n是点的数量,m是边的数量
int ans[M],cnt;//存答案
int st;
void init(){//reset
    n=m=0;
    memset(g,0,sizeof g);
    memset(d,0,sizeof d);
}
bool input(){
    init();//初始化
    int a,b,c;
    cin>>a>>b;
    if(!a&&!b)return 0;
    cin>>c;
    n=max(n,max(a,b));//确定最大编号
    st=min(a,b);//题目说起点是第一次输入的最小值
    g[a][c]=b;
    g[b][c]=a;
    d[a]++;
    d[b]++;
    m++;//
    while(cin>>a>>b){
        if(!a&&!b)break;
        cin>>c;
        n=max(n,max(a,b));//确定最大编号
        g[a][c]=b;
        g[b][c]=a;
        d[a]++;
        d[b]++;
        m++;
    }
    return 1;
}
void dfs(int x){//传的值是点
    for(int i=1;i<=m;i++){
        if(g[x][i]&&!mark[i]){
            mark[i]=1;
            dfs(g[x][i]);
            ans[++cnt]=i;
        }
    }
}
void solve(){
    for(int i=1;i<=n;i++){
        if(d[i]%2){//如果有奇数度,就不存在欧拉回路
            puts("Round trip does not exist.");
            return ;
        }
    }
    memset(mark,0,sizeof mark);
    cnt=0;
    dfs(st);
    for(int i=m;i>=1;i--){
        cout<<ans[i]<<' ';
    }
    cout<<endl;
}
int main(){
//    ios::sync_with_stdio(0);
    while(input()){
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值