poj 1041(字典序输出欧拉回路)

John's trip
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8641   Accepted: 2893   Special 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

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.

题意:小明要从自己家走遍每一条街道有且仅只有一次访问每一个朋友然后回到自己家,这些路径都有一个权值,按照这些路径的字典序输出走法.

题解:很明显的欧拉回路,首先判断是否为欧拉回路,即每个点的度都是偶数,如果满足欧拉回路,先对每个结点所连接的边按照边的编号排序,这里利用 vector 中的 pair是最好不过了.然后进行一次深搜就行了.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
const int N = 5000;
vector < pair<int,int> > edge[N];
bool vis[N];  ///标记哪些边被访问过了
int in[N];
int out[N],ans[N],cnt;
void init()
{
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(vis,false,sizeof(vis));
    for(int i=0;i<N;i++) edge[i].clear();
    cnt = 0;
}
void addEdge(int u,int v,int w){
    edge[u].push_back(make_pair(w,v));
    edge[v].push_back(make_pair(w,u));
}
void dfs(int u){
    for(int i=0;i<edge[u].size();i++){
        int e = edge[u][i].first;
        int v = edge[u][i].second;
        if(!vis[e]){
            vis[e] = true;
            dfs(v);
            ans[cnt++] = e;
        }
    }
}
bool cmp(pair<int,int> a,pair<int,int> b){
    return a.first<b.first;
}
int main()
{
    int x,y,z,m;
    while(scanf("%d%d",&x,&y)!=EOF)
    {
        int MIN = N,m=0;
        if(x==0&&y==0) break;
        scanf("%d",&z);
        init();
        in[x]++,out[x]++;
        in[y]++,out[y]++;
        addEdge(x,y,z);
        MIN = min(MIN,min(x,y));
        while(scanf("%d%d",&x,&y)!=EOF)
        {
            if(x==0&&y==0) break;
            scanf("%d",&z);
            in[x]++,out[x]++;
            in[y]++,out[y]++;
            addEdge(x,y,z);
            MIN = min(MIN,min(x,y));
        }
        bool flag = false;
        for(int i=1;i<N;i++){
            if(in[i]%2==1||out[i]%2==1){
                flag = true;
                break;
            }
            if(edge[i].size()) sort(edge[i].begin(),edge[i].end(),cmp);
        }
        if(flag) {
            printf("Round trip does not exist.\n");
            continue;
        }
        dfs(MIN);
        for(int i=cnt-1;i>=0;i--){
            printf("%d ",ans[i]);
        }
        printf("\n");
    }
}

 



posted @ 2016-10-13 20:41  樱花庄的龙之介大人  阅读(233)  评论(0编辑  收藏  举报