John's trip

来源:互联网 发布:linux显示目录树 编辑:程序博客网 时间:2024/06/12 18:55

<div><a target=_blank href="http://poj.org/problem?id=1041">http://poj.org/problem?id=1041</a></div><div>Description</div><div></div><div>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. <wbr></wbr></div><div></div><div>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. <wbr></wbr></div><div></div><div>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 <wbr></wbr></div><div>Input</div><div></div><div>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.</div><div>Output</div><div></div><div>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."</div><div>Sample Input</div><div></div><div>1 2 1</div><div>2 3 2</div><div>3 1 6</div><div>1 2 5</div><div>2 3 3</div><div>3 1 4</div><div>0 0</div><div>1 2 1</div><div>2 3 2</div><div>1 3 3</div><div>2 4 4</div><div>0 0</div><div>0 0</div><div>Sample Output</div><div></div><div>1 2 3 5 4 6 <wbr></wbr></div><div>Round trip does not exist.</div><div></div><div>欧拉回路:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路。一个无向图存在欧拉回路的充要条件是每个顶点的度是偶数,对于有向图存在欧拉回路的条件是每个顶点的出度等于入度(就是出去的边数等于进来的边数)。根据这个首先判断存在欧拉回路不,如果存在然后用DFS去找欧拉回路。DFS的思想等效于先找一个环,然后对环上所有点递归DFS,并且把这些递归产生的路插入这个环中。实际上程序实现起来很简单,递归完成后不需要单独做插入。</div><div></div><div></div><div>由于图已经保证连通,首先用度数是否是偶数,判断图是否是欧拉图,然后,输出最小升序,其实就是每次都从小往大的搜,先搜得一个最小序环,然后对环上的每一点进行搜索,其实对于欧拉图而言,每个点要么就只剩一个点,什么也搜不到了,要么还有一个环,只要把环上路径全都插入到对应位置上,用栈存路径,每次只有回溯到当前点,就是说当前点的后继都已经搜过了的时候,才把当前点入栈,这样一来倒着输出,就能得到一个欧拉回路,而且是最小升序。</div>
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <vector>#include <stack>#include <queue>#include <set>#include <time.h>#include<fstream>using namespace std;int path[2000], g[2000][2000], flag[2000], n, m, k;void dfs(int v){int i;for (i = 1; i <= m; i++){if (!flag[i] && g[v][i]){flag[i] = 1;dfs(g[v][i]);path[k++] = i;}}}int main(){int i;int x, y, z, start;fstream cin;cin.open("in21.txt");while (cin >> x >> y && (x || y)){memset(g, 0, sizeof(g));memset(flag, 0, sizeof(flag));memset(path, 0, sizeof(path));k = n = m = 0;start = 2000;do{cin >> z;g[x][z] = y;g[y][z] = x;g[x][0]++;g[y][0]++;m = m<z ? z : m;n = (x>y ? x : y)>n ? (x>y ? x : y) : n;start = (x<y ? x : y)<start ? (x<y ? x : y) : start;} while (cin >> x >> y && (x || y));for (i = 1; i <= n; i++){if (g[i][0] % 2){printf("Round trip does not exist.\n");break;}}if (i == n + 1){dfs(start);for (int j = k - 1; j >= 0; j--){printf("%d", path[j]);if (j != 0){printf(" ");}else{printf("\n");}}}}return 0;}


0 0
原创粉丝点击