zoukankan      html  css  js  c++  java
  • 第二周习题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;
    }
  • 相关阅读:
    mysql数据库常用指令
    解决windows的mysql无法启动 服务没有报告任何错误的经验。
    “Can't open file for writing”或“operation not permitted”的解决办法
    启动Apache出现错误Port 80 in use by "Unable to open process" with PID 4!
    如何打开windows的服务services.msc
    常见的HTTP状态码 404 500 301 200
    linux系统常用的重启、关机指令
    (wifi)wifi移植之命令行调试driver和supplicant
    linux(debian)安装USB无线网卡(tp-link TL-WN725N rtl8188eu )
    alloc_chrdev_region申请一个动态主设备号,并申请一系列次设备号
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4692974.html
Copyright © 2011-2022 走看看