poj1041 John's trip (欧拉回路)
来源:互联网 发布:郑元畅为什么不红 知乎 编辑:程序博客网 时间:2024/06/06 01:48
John’s trip
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 8400Accepted: 2832Special 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
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.这道题主要就是字典序,然后图是联通的,不用判断,判断度数就行了
发现网上很多代码都是一样的,难道大家的思路都是一模一样?
自己写了一个,比较戳
主要就是dfs每次排序,其实可以一开始就排好,然后再建图的
#include<iostream>using namespace std;#include<vector>#include<cstring>#include<cstdio>#include<queue>#include<algorithm>#define sii(x,y) scanf("%d%d",&(x),&(y))#define si(x) scanf("%d",&(x))const int maxn=3991;struct note { int to,next,s;} a[maxn];int head[maxn],top;int used[maxn];int n,m;void ADD(int u,int v,int w) { a[top].next=head[u]; a[top].to=v; a[top].s=w; head[u]=top++;}int first;void dfs(int now) { vector<pair<int,int> > pu; pu.clear(); for(int i=head[now]; ~i; i=a[i].next) { if(!used[i]) { pu.push_back(make_pair(a[i].s,i)); } } sort(pu.begin(),pu.end(),greater<pair<int,int> >() ); int len=(int)pu.size(); for(int j=0; j<len; ++j) { int i=pu[j].second; if(!used[i]) { used[i]=used[i^1]=1; dfs(a[i].to); if(!first)putchar(' '); first=0; printf("%d",a[i].s); } }}int main() { int u,v,w;#ifdef tangge freopen("1041.txt","r",stdin);#endif // tangge while(true) { sii(u,v); if(!(u||v)) { break; } si(w); n=0; n=max(n,max(u,v)); memset(head,-1,sizeof(head)); memset(used,0,sizeof(used)); top=0; int st; ADD(u,v,w),ADD(v,u,w); st=min(u,v); ++used[v],++used[u]; while(sii(u,v)&&((u||v))) { si(w); ADD(u,v,w),ADD(v,u,w); ++used[v],++used[u]; n=max(n,max(u,v)); } bool J=true; for(int i=1; i<45; ++i) { if(used[i]&1) { puts("Round trip does not exist."); J=false; break; } } if(!J)continue; memset(used,0,sizeof(used)); first=1; dfs(st); putchar(10); } return 0;}
0 0
- poj1041 John's trip (欧拉回路)
- POJ1041 John's trip 欧拉回路路径查找
- poj1041 John's trip(欧拉回路+输出路径)
- POJ1041 John's trip(欧拉回路+打印路径)
- POJ1041 John's trip(欧拉回路 + 并查集 + dfs)
- POJ1041 John's trip
- POJ1041 John's trip
- poj1041-John's trip
- poj1041 John's trip,无向图求欧拉回路路径
- poj1041 John's trip (无向图求欧拉回路方案)
- POJ 1041 John's trip (欧拉回路)
- John's trip 欧拉回路输出路径
- poj 1041 John's trip(欧拉回路)
- POJ 1041 John's trip(欧拉回路+输出路径)
- poj 1041 John's trip 欧拉回路
- 【欧拉回路】toj 1004 John's trip
- POJ 1041 John's trip (欧拉回路)
- POJ 1041 John's trip(欧拉回路)
- 苹果导出P12文件
- maven的多环境打包部署与web.xml整合
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- neural-networks-and-deep-learning test.py
- DHCP与PPPOE 区别
- poj1041 John's trip (欧拉回路)
- lintcode construct-binary-tree-from-inorder-and-postorder-traversal 中序遍历和后序遍历树构造二叉树
- HttpURLConnection与 HttpClient 区别
- 牛顿法与拟牛顿法
- LeetCode Unique Binary Search Trees 2 递归方法
- geekband android #5 第一周分享(初识Android)
- 无法执行添加/移除操作,因为代码元素**是只读的
- oracle的分析函数over及开窗关键字range
- 【SSH进阶之路】Spring的IOC逐层深入——依赖注入的两种实现类型