程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1041 無向圖的歐拉回路

POJ 1041 無向圖的歐拉回路

編輯:C++入門知識

John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6569 Accepted: 2182 Special 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.


如果存在,輸出最小字典序解,

比較簡單的歐拉回路,

代碼:

/* ***********************************************
Author :rabbit
Created Time :2014/3/24 21:38:47
File Name :12.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=2000;
const int maxm=50;
struct node{
	int s,t;
}r[maxn];
int vis[maxn],deg[maxn],s[maxn];
int n,S,top;
void dfs(int now){
	for(int i=1;i<=n;i++)
		if(!vis[i]&&(r[i].s==now||r[i].t==now)){
			vis[i]=1;
			dfs(r[i].s+r[i].t-now);
			s[++top]=i;
		}
}
bool exist(){
	for(int i=1;i<=maxn-1;i++)
		if(deg[i]%2==1)return 0;
	return 1;
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     int x,y,num;
	 while(cin>>x>>y&&x){
		 S=min(x,y);n=0;
		 memset(deg,0,sizeof(deg));
		 cin>>num;
		 r[num].s=x;r[num].t=y;
		 deg[x]++;deg[y]++;
		 n=max(n,num);
		 while(cin>>x>>y&&x){
			 S=min(S,min(x,y));
			 cin>>num;
			 r[num].s=x;r[num].t=y;
			 deg[x]++;deg[y]++;
			 n=max(n,num);
		 }
		 if(exist()){
			 top=0;
			 memset(vis,0,sizeof(vis));
			 dfs(S);
			 for(int i=top;i>=2;i--)cout<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved