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;
    }
  • 相关阅读:
    HDU 1224 无环有向最长路
    HDU 1220 简单数学题
    HDU 1203 背包问题
    HDU 1176 DP
    HDU 1159 LCS最长公共子序列
    HDU 1160 排序或者通过最短路两种方法解决
    hdu 2349 最小生成树
    次小生成树的学习
    最小生成树prime算法模板
    poj 1679 判断最小生成树是否唯一
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4692974.html
Copyright © 2011-2022 走看看