zoukankan      html  css  js  c++  java
  • 无向图欧拉回路

    1041

    John's trip
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6993   Accepted: 2302   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.
    题意:无向图,要经过每一条路线仅且一次,并且回到原点,起始点是编号最小的点;

    首先判断是否联通

    其次判断该连通图是否满足欧拉回路的条件,即所有点的度为偶数

    然后用dfs判定

    题目还要求输出路径的编号要求字典序最小,先把编号从大到小排列即可

    程序:

    #include"stdio.h"
    #include"string.h"
    #include"queue"
    #include"stack"
    #include"iostream"
    #include"string"
    #include"map"
    #include"stdlib.h"
    #define inf 99999999
    #define M 1009
    using namespace std;
    struct st
    {
        int u,v,w,next,vis,id;
    }edge[M*6],Stack[M*6];
    int t,top,head[M],out[M],in[M],dis[M],s[M],cnt,use[M];
    struct node
    {
        int a,b,c;
    }p[M*6];
    int cmp(const void *a,const void *b)
    {
        return (*(struct node*)b).c-(*(struct node*)a).c;
    }
    void init()
    {
        t=0;
        memset(head,-1,sizeof(head));
        memset(edge,0,sizeof(edge));
    }
    void add(int u,int v,int w)
    {
        edge[t].u=u;
        edge[t].v=v;
        edge[t].w=w;
        edge[t].next=head[u];
        head[u]=t++;
    }
    /*void dfs(int u)//递归写法
    {
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(!edge[i].vis)
            {
                edge[i].vis=edge[i^1].vis=1;
                dfs(v);
                s[cnt++]=edge[i].w;
            }
        }
    }*/
    void dfs(int u)//非递归写法
    {
        int i ;
        top=0;
        Stack[top].u=u;
        Stack[top].id=head[u];
        top++;
        while(top>0)
        {
            st x=Stack[top-1];
            for(i=head[x.u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v;
                if(!edge[i].vis)
                {
                    edge[i].vis=edge[i^1].vis=1;
                    Stack[top].u=v;
                    Stack[top].id=i;
                    top++;
                    break;
                }
            }
            if(i==-1)
            {
                //printf("%d ",x.u);
                if(top-1>0)
                s[cnt++]=edge[x.id].w;
                top--;
            }
        }
    }
    int main()
    {
        int A,B,C,i,j;
        while(scanf("%d%d",&A,&B),A||B)
        {
            int m=0;
            int start=inf;
            memset(use,0,sizeof(use));
            init();
            scanf("%d",&C);
            p[m].a=A;
            p[m].b=B;
            p[m++].c=C;
            start=min(start,A);
            start=min(start,B);
            use[A]=use[B]=1;
            while(scanf("%d%d",&A,&B),A||B)
            {
                scanf("%d",&C);
                p[m].a=A;
                p[m].b=B;
                p[m++].c=C;
                start=min(start,A);
                start=min(start,B);
                use[A]=use[B]=1;
            }
            qsort(p,m,sizeof(p[0]),cmp);
            for(i=0;i<m;i++)
            {
                add(p[i].a,p[i].b,p[i].c);
                add(p[i].b,p[i].a,p[i].c);
            }
            int num=0,degree;
            for(i=1;i<=44;i++)
            {
                if(use[i])
                {
                    degree=0;
                    for(j=head[i];j!=-1;j=edge[j].next)
                    {
                        degree++;
                    }
                    if(degree&1)
                        num++;
                }
            }
            if(num==0)
            {
                cnt=0;
                dfs(start);
                for(i=cnt-1;i>=0;i--)
                {
                    if(i==cnt-1)
                        printf("%d",s[i]);
                    else
                        printf(" %d",s[i]);
                }
                printf("
    ");
            }
            else
                printf("Round trip does not exist.
    ");
    
    
    
        }
    }
    


  • 相关阅读:
    2. jvm-sandbox之结构和状态
    1. jvm-sandbox之小试牛刀
    redux中间件来执行异步操作
    redux
    vue简单插件
    VUE搭建
    用element-ui进行文件的上传
    完整的node脚手架搭建服务
    使用node来搭建简单的后台业务
    解决vuex数据页面刷新后初始化问题
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348208.html
Copyright © 2011-2022 走看看