• 第二周习题O题


    Description

    Download as PDF
     

    Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it is connected in the graph. The bandwidth of the ordering is then defined as the maximum of the individual bandwidths. For example, consider the following graph:

    picture25

    This can be ordered in many ways, two of which are illustrated below:

    picture47

    For these orderings, the bandwidths of the nodes (in order) are 6, 6, 1, 4, 1, 1, 6, 6 giving an ordering bandwidth of 6, and 5, 3, 1, 4, 3, 5, 1, 4 giving an ordering bandwidth of 5.

    Write a program that will find the ordering of a graph that minimises the bandwidth.

     

    Input will consist of a series of graphs. Each graph will appear on a line by itself. The entire file will be terminated by a line consisting of a single #. For each graph, the input will consist of a series of records separated by `;'. Each record will consist of a node name (a single upper case character in the the range `A' to `Z'), followed by a `:' and at least one of its neighbours. The graph will contain no more than 8 nodes.

     

    Output will consist of one line for each graph, listing the ordering of the nodes followed by an arrow (->) and the bandwidth for that ordering. All items must be separated from their neighbours by exactly one space. If more than one ordering produces the same bandwidth, then choose the smallest in lexicographic ordering, that is the one that would appear first in an alphabetic listing.

     

    A:FB;B:GC;D:GC;F:AGH;E:HD
    #

     

    A B C F G D H E -> 3

    这道题没什么特别的,深搜里多加个条件,即有从此路到彼路的路

    #include"iostream"
    #include"cstring"
    #include"cstdio"
    #include"map"
    #include"algorithm"
    using namespace std;
    
    map<char,map<char,int> >m;
    
    int len,w,ans;
    int book[100];
    int a[100];
    int an[100];
    
    int count(char *p)
    {
        int j=0;
    for(char i='A';i<='Z';i++)
    if(strchr(p,i))
    {
    
    
      j++;
    
    }
    
    return j;
    }
    
    
    void DFS(int step, int bw)
    {
        if(step==len)
        {
            ans=bw;
            memcpy(an,a,100);
            return;
        }
    
        for(int i=0;i<len;i++)
        {
            if(!book[i])
            {
                book[i]=1;
                a[step]=i;
                int w=0;
                for(int j=0;j<step;j++)
                {
                    if(m[i+'A'][a[j]+'A'])
                    {
                        w=step-j;
                        break;
                    }
                }
                int ibw=max(bw, w);
                if(ibw<ans)
                    DFS(step+1, ibw);
                book[i]=0;
            }
        }
    }
    int main()
    {
        char ab[300];
        while(gets(ab) && ab[0]!='#')
        {
            len=count(ab);
            char*p=strtok(ab, ";");
            while(p)
            {
                int t=p[0];
                ++p;
                while(*(++p))
                {
                    m[t][*p]=1;
                    m[*p][t]=1;
                }
                p=strtok(0, ";");
            }
    
            ans=len;
            DFS(0, 0);
    
            for(int i=0;i<len;i++)
            {
                printf("%c ", an[i]+'A');
            }
            printf("-> %d
    ", ans);
        }
    
        return 0;
    }
  • 相关阅读:
    谷歌浏览器插件开发Tutorial: Getting Started (Hello, World!) 教程:准备开始(你好,世界!)
    Android ViewPager多页面滑动切换以及动画效果
    4.4 我同意条款—CheckBox的isCheck属性
    4.2设计具有背景图的按钮—ImageButton的焦点及事件处理
    【文件打开】浏览打开窗口
    【原创】PE检测工具
    emu8086注册算法分析及KeyGen实现
    学破解 <一> PE格式之MSDOS MZ header
    学破解 <二> PE格式之IMAGE_NT_HEADERS
    反虚拟机程序测试
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4692974.html
Copyright © 2020-2023  润新知