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;
    }
  • 相关阅读:
    私有云是伪命题:真正的私有云 ≈ 公有云
    云计算的重新构建架构:优化迁移策略
    五个顶级的大数据架构
    Algorithm Gossip: 费式数列
    Algorithm Gossip: 河内塔
    Mysql连接报错:Unknown system variable 'language'
    ssm整合的时候出现的abstactMethodArror 解决
    java集合整理
    Oracle的序列和索引
    关于java堆栈的理解与说明
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4692974.html
Copyright © 2011-2022 走看看