poj-1041-John's trip

来源:互联网 发布:unity3d工程师薪资 编辑:程序博客网 时间:2024/06/16 14:06

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


如果你英语足够好,你会发现以下条件:

1.有n条街,m个路口(然而有重边...)

2.每条路刚好走一次然后回到一开始在的地方

3.Johnny最开始在每组输入数据第一行中xy的较小值

4.这道题有spj(题干似乎没说但确实有)

5.输入输出参见样例







然后这根本就是一道赤裸裸的欧拉回路(自行参照条件2)嘛...

然后发现有哪个点的度数为奇数的时候就可以直接断定没有符合条件的路了..(要么不能刚好走一次要么刚好走完一次之后在另一个点停下)这可能是一道小学奥数题..

所以就直接看代码吧...(多组数据还是习惯性memset然后说每个路口最多连44条街这事写的时候压根没注意所以就有了这么鬼畜的数组)

严重怀疑这道题出在1995年4月4日...多么不吉利??

#include<cstdio>#include<cstring>int num[50],jc[50][2000],st[50][2000];bool vis[2000];void dfs(int u){int i;for(i=1;i<=num[u];i++){if(vis[st[u][i]]) continue;vis[st[u][i]]=1;dfs(jc[u][i]);printf("%d ",st[u][i]);}}int main(){int i,x,y,z,s;while(~scanf("%d%d",&x,&y)&&x&&y){s=x<y?x:y;memset(num,0,sizeof(num));memset(vis,0,sizeof(vis));memset(jc,0,sizeof(jc));memset(st,0,sizeof(st));scanf("%d",&z);jc[x][++num[x]]=y;st[x][num[x]]=z;jc[y][++num[y]]=x;st[y][num[y]]=z;while(~scanf("%d%d",&x,&y)&&x&&y) {scanf("%d",&z);jc[x][++num[x]]=y;st[x][num[x]]=z;jc[y][++num[y]]=x;st[y][num[y]]=z;}for(x=0,i=1;i<=44;i++) if(num[i]%2) {x=1;printf("Round trip does not exist.\n");break;}if(x) continue;dfs(s);printf("\n");}}

插播一段题外话:

似乎我的暑假还没开始就已经结束了..暑假?不存在的

一半c++一半数理化生..(没办法学校的什么夏令营嘛我爱学习学习使我快乐!!)

然后因为种种原因就成为了打野的OIer..(2333我喜欢)

_(:з」∠)_


原创粉丝点击