首页 > 代码库 > POJ 1041 John's trip
POJ 1041 John's trip
John‘s trip
Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 104164-bit integer IO format: %lld Java class name: Main
Special Judge
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
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 12 3 23 1 61 2 52 3 33 1 40 01 2 12 3 21 3 32 4 40 00 0
Sample Output
1 2 3 5 4 6 Round trip does not exist.
Source
Central Europe 1995
解题:求欧拉回路,还要字典序最小。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 2010;18 int g[50][maxn],degree[50],stk[maxn],tp,n,m;19 bool vis[maxn];20 void dfs(int u) {21 for(int i = 1; i <= n; i++) {22 if(!vis[i] && g[u][i]) {23 vis[i] = true;24 dfs(g[u][i]);25 stk[tp++] = i;26 }27 }28 }29 int main() {30 int x,y,z,st,i;31 while(scanf("%d %d",&x,&y),x||y) {32 n = m = tp = 0;33 memset(vis,false,sizeof(vis));34 memset(degree,0,sizeof(degree));35 memset(g,0,sizeof(g));36 st = min(x,y);37 scanf("%d",&z);38 n = max(n,z);39 m = max(m,max(x,y));40 g[x][z] = y;41 g[y][z] = x;42 degree[x]++;43 degree[y]++;44 while(scanf("%d %d",&x,&y),x||y) {45 scanf("%d",&z);46 n = max(n,z);47 m = max(m,max(x,y));48 g[x][z] = y;49 g[y][z] = x;50 degree[x]++;51 degree[y]++;52 }53 for(i = 1; i <= m; i++)54 if(degree[i]&1) break;55 if(i <= m) puts("Round trip does not exist.");56 else {57 dfs(st);58 tp--;59 printf("%d",stk[tp--]);60 while(tp >= 0) printf(" %d",stk[tp--]);61 puts("");62 }63 }64 return 0;65 }66 /*67 1 2 168 2 3 269 3 1 670 1 2 571 2 3 372 3 1 473 0 074 */
POJ 1041 John's trip
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。