poj_1041 John's trip
来源:互联网 发布:linux开启smb服务 编辑:程序博客网 时间:2024/05/21 05:37
John's trip
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 5455
Accepted: 1774
Special Judge
题目链:http://poj.org/problem?id=1041
Description
Little Johnny has got a new car. He decided to drivearound 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 startedthinking how to make his trip as short as possible. Very soon he realized thatthe 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 hisparents' 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 townhad different numbers. Each street was connecting exactly two junctions. No twostreets in the town had the same number. He immediately started to plan hisround trip. If there was more than one such round trip, he would have chosenthe one which, when written down as a sequence of street numbers islexicographically the smallest. But Johnny was not able to find even one suchround trip.
Help Johnny and write a program which finds the desired shortest round trip. Ifthe round trip does not exist the program should write a message. Assume thatJohnny lives at the junction ending the street appears first in the input withsmaller number. All streets in the town are two way. There exists a way fromeach street to another street in the town. The streets in the town are verynarrow and there is no possibility to turn back the car once he is in thestreet
Input
Input file consists of several blocks. Each blockdescribes 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 bythe 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 ofstreet numbers (single members of the sequence are separated by space)describing Johnny's round trip. If the round trip cannot be found thecorresponding output block contains the message "Round trip does notexist."
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
Central Europe 1995
题意:
x,y,z表示起点,终点和这条边的编号,从图的起点出发,问是否能访问完所有边,然后回到起点,访问的相同边根据字典序列来访问。
解题思路:
这个题目可以用欧拉回路来决解,首先根据输入建立图,保存图中每条边的编号,起点,终点,记录边数和点数,用欧拉回路来判断该图是否存在欧拉回路,如果存在,则递归遍历每一条边,直到所有边都遍历完成。遍历的起点为上一条边的终点。
代码:
#include <iostream>#include<stdio.h>#include<string.h>#define MAX 50using namespace std;struct edge{//存边 int start,end;};edge e[2000];//边数int g[MAX][MAX];int indegree[MAX];int outdegree[MAX];int temp[MAX];int visited[2000];//记录某节点是否被访问过int t;int v,es;//记录点数和边数//从起点s开始遍历每一条边void euler(int s){ for(int i=1;i<=es;i++) { //该条边没有被访问过,并且该边的起点或者终点是要访问的点s if(visited[i]==0 && (e[i].start==s||e[i].end==s)) { visited[i]=1;//访问该条边 euler(e[i].start+e[i].end-s);//从起点访问到终点,再以终点为起点开始访问下一条边 temp[t]=i; t++; } }}int main(){ int x,y,z; int i,j; int flag=0; while(true) { int start; memset(g,0,sizeof(g)); memset(visited,0,sizeof(visited)); memset(indegree,0,sizeof(indegree)); memset(outdegree,0,sizeof(outdegree)); v=0; es=0; t=0; while(true) { scanf("%d%d",&x,&y); if(x==0 && y==0) { flag++; if(flag==2) return 0; break; } //求出起点,x,y中的最小值 start=x; if(start>y) start=y; flag=0; scanf("%d",&z); g[x][y]=1; g[y][x]=1; e[z].start=x;//图已经的连通图,不用再判断是否连通 e[z].end=y; //计算边数和点数 if(es<z) es=z; if(v<x) v=x; if(v<y) v=y;//记录出度和入度,用来判断欧拉路 indegree[x]++; outdegree[y]++; } //判断是否是欧拉回路 for(i=1;i<=v;i++) { for(j=1;j<=v;j++) { if(g[i][j]==1 && (outdegree[j]+indegree[j])%2!=0) { printf("Round trip does not exist.\n"); break; } } if(j<=v) break; } if(i<=v) continue; euler(start); for(i=es-1;i>=1;i--) { printf("%d ",temp[i]); } printf("%d\n",temp[0]); } return 0;}
- poj_1041 John's trip
- PKU1041 John's trip
- POJ1041 John's trip
- POJ1041 John's trip
- John's trip
- John's trip
- John's trip
- poj1041-John's trip
- POJ 1041 John's Trip
- poj 1041 John's trip
- POJ 1041 John's trip
- poj 1041 John's trip
- POJ 1041 John's trip
- John's trip problem python
- POJ-1041-John's trip
- POJ 1041 - John's trip
- poj 1041 John's trip
- POJ-1041-John's trip
- 用jquery.form异步提交页面时,服务器端取不到Ueditor的值
- S3C2440 LED 驱动程序详解
- 面向对象和面向过程的区别
- 博客搬家
- 浅谈ARM裸板调试
- poj_1041 John's trip
- [已删除]
- 使用NSFileManager枚举目录种的内容(遍历目录)
- poj_1125 Stockbroker Grapevine
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- MFC中动态创建多个按钮并实现其消息响应
- poj_1797 Heavy Transportation
- delphiIDE调试使用小结
- Discuz 。net版下载地址: