• 第二周习题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;
    }
  • 相关阅读:
    14丨指令的本质是什么
    Nodejs的测试和测试驱动开发
    javascript的Mixins
    踏破铁鞋无觅处,从AsyncTask学Android线程池
    Android自定义视图四:定制onMeasure强制显示为方形
    Android自定义视图三:给自定义视图添加“流畅”的动画
    Android自定义视图二:如何绘制内容
    Android自定义视图一:扩展现有的视图,添加新的XML属性
    属性动画和Activity、Fragment过渡动画等
    RecyclerView怎么能没有ItemClickListener?加一个!
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4692974.html
Copyright © 2020-2023  润新知