POJ1041 John's trip

John's trip

Language:
John's trip
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 11092Accepted: 3796Special Judge

Description

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.

Source

John有很多朋友住在不同的街,John想去访问每位朋友,同时希望走的路最少。因为道路很窄,John在一条路上不能往回走。John希望从家里出发,拜访完所有的朋友后回到自己的家,且总的路程最短。John意识到如果可以每条道路都只走一次然后返回起点应该是最短的路径。写一个程序帮助John找到这样的路径。给出的每条街连接两个路口,最多有1995条街,最多44个路口。街编号由1到n, 路口分别编号1到m.

题解

就是判欧拉图找欧拉回路,递归改成迭代(这题不必要)。

1995年的题读入方式非常古怪。

#include<iostream>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
    for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;

co int N=4e3;
int n,m;
int head[N],ver[N],edge[N],next[N],tot;
int deg[N],stack[N],ans[N],top,t;
bool vis[N];

il void add(int x,int y,int z){
	ver[++tot]=y,edge[tot]=z,next[tot]=head[x],head[x]=tot;
}
void euler(){
	stack[++top]=2,vis[2]=vis[3]=1;
	while(top){
		int e=stack[top],x=ver[e],i=head[x];
		while(i&&vis[i]) i=next[i];
		if(i){
			stack[++top]=i;
			head[x]=next[i];
			vis[i]=vis[i^1]=1;
		}
		else ans[++t]=edge[stack[top--]];
	}
}
int main(){
	int x,y,z;
	while(read(x)|read(y)){
		tot=1,t=0,top=0;
		memset(deg,0,sizeof deg);
		memset(head,0,sizeof head);
		memset(vis,0,sizeof vis);
		do{
			read(z);
			add(x,y,z),add(y,x,z);
			++deg[x],++deg[y];
		}while(read(x)|read(y));
		bool flag=0;
		for(int i=1;i<=50;++i)
			if(deg[i]&1) {flag=1;break;}
		if(flag){
			puts("Round trip does not exist.");
			continue;
		}
		euler();
		for(int i=t;i>1;--i) printf("%d ",ans[i]);
		printf("%d
",ans[1]);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/autoint/p/11047419.html