John's trip(POJ 1041)

题目链接

题目描述

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 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 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.”

输入样例

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

输出样例

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

分析

题目大意对于每一个块中边构成的图,要求判断是否存在欧拉回路,如果存在欧拉回路输出最小字典序,否则输出“Round trip does not exist.”
首先判断图是否连通且是否存在奇度点,如果图连通且没有奇度点,则存在欧拉回路,按照Fleury算法思想和最小序号搜索欧拉回路;否则,图中不存在欧拉回路,输出“Round trip does not exist.”。

Fleury 算法

Fleury 算法是用于求无向图中欧拉回路的算法,其基本思想如下:
任取图 G 中一顶点 v0,令 P0=v0
假设沿 Pi=v0e1v1e2…eivi,走到顶点 vi,按下面的方法从 E(G)-{e1,e2,…,ei} 中选择 ei+1:
1)ei+1 与 vi 相关联
2)除非无别的边可供选择,否则 ei+1 不应是 Gi=G-{e1,e2,…,ei} 中的桥
当 2 不能再进行时,算法停止
可以证明的是,当算法停止时,所得到简单回路 Pm=v0e1v1e2…emvm(vm=v0) 为G中的欧拉回路

源程序

//#include <bits/stdc++.h>	poj不能使用万能头 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define MAXN 50
using namespace std;
struct Node{
	int v,data;
};
vector<Node> g[MAXN];
int x,y,z,start,nmax,k;
int degree[MAXN],father[MAXN],path[2005];
bool used[2005];
int cmp(Node a,Node b)
{
	return a.data<b.data;
}
int find(int x)
{
	if(x==father[x])return x;
	return father[x]=find(father[x]);
}
void Union(int x,int y)
{
	x=find(x);
	y=find(y);
	if(x!=y)father[x]=y;
}
void euler(int u)
{
	int n=g[u].size();
	for(int i=0;i<n;i++){
		int v=g[u][i].v,num=g[u][i].data;
		if(!used[num]){
			used[num]=true;
			euler(v);
			path[++k]=num;
		}
	}
}
int main()
{
	while(scanf("%d%d",&x,&y)&&(x||y)){
		memset(used,0,sizeof(used));
		for(int i=1;i<=44;i++){
			degree[i]=0;
			father[i]=i;
			g[i].clear();
		} 
		k=0,start=min(x,y);	//边数、欧拉回路起点 
		do{		//初始化 
			scanf("%d",&z);
			g[x].push_back(Node{y,z});
			g[y].push_back(Node{x,z});
			degree[x]++;
			degree[y]++;
			Union(x,y);
		}while(scanf("%d%d",&x,&y)&&(x||y));
		int cnt=0;	//统计连通分量 
		for(int i=1;i<=44;i++)
			if(degree[i]&&i==father[i])
				cnt++;
		if(cnt>1) {
			printf("Round trip does not exist.\n");
			continue;
		}
		bool flag=false;	//标记是否存在奇度点 
		for(int i=1;i<=44;i++)
			if(degree[i]&1) flag=true;
			else sort(g[i].begin(),g[i].end(),cmp);
		if(flag) printf("Round trip does not exist.\n");
		else{
			euler(start);
			for(int i=k;i>=1;i--)
				printf("%d ",path[i]);
			printf("\n");
		}
	}
}
发布了19 篇原创文章 · 获赞 0 · 访问量 133

猜你喜欢

转载自blog.csdn.net/weixin_43960284/article/details/105237177