zoukankan      html  css  js  c++  java
  • UVA

     Bandwidth 

    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 vis 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

    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

    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.

    Sample input

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

    Sample output

    A B C F G D H E -> 3
    
    
    
    


    生成排列问题,记录节点和道路,枚举节点的排列,计算每个排列的带宽。注意计算带宽的过程如果大于等于最小带宽,要及时跳出。
    题目只用的22MS, 貌似还可以

    #include <bits/stdc++.h>
    using namespace std;
    
    int cnt = 0;
    int perm[26], res[26];
    int dot[26], beg;
    int road[26][26];
    int Min, Max;
    
    int main()
    {
        string line;
        while(getline(cin, line), line!="#")
        {
            memset(road, 0, sizeof(road));
            memset(dot, 0, sizeof(dot));
            for(size_t i = 0; i != line.size(); i++)
            {
                if(line[i]==':' || line[i]==';')
                    continue;
                if(line[i+1]==':')
                    beg = line[i] - 'A';
                else{
                    road[beg][line[i]-'A'] = 1;
                    road[line[i]-'A'][beg] = 1;
                }
                if(isalpha(line[i]))
                    dot[line[i]-'A'] = 1;
            }
            cnt = 0;
            for(size_t i = 0; i < 26; i++)
                if(dot[i])
                    perm[cnt++] = i;
    
            sort(perm, perm+cnt);
            Min = INT_MAX;
            do{
                Max = INT_MIN;
                for(int i = 0; i < cnt-1; i++)
                    for(int j = i+1; j < cnt; j++)
                        if(road[perm[i]][perm[j]]){
                            Max = max(Max, j-i);
                            if(Max >= Min)
                                goto loop;
                        }
                if(Max < Min){
                    Min = Max;
                    memcpy(res, perm, sizeof(perm));
                }
                loop:;
            }while(next_permutation(perm, perm+cnt));
    
            for(int i = 0; i < cnt; i++)
                printf("%c ", res[i]+'A');
            printf("-> %d
    ", Min);
        }
    }


  • 相关阅读:
    10 行 Python 代码,批量压缩图片 500 张,简直太强大了
    听说苏州是互联网的荒漠,真的吗?
    sum() 函数性能堪忧,列表降维有何良方?
    len(x) 击败 x.len(),从内置函数看 Python 的设计思想
    如何给列表降维?sum()函数的妙用
    Shell脚本关于循环的一些总结
    大技霸教你远程执行Linux脚本和命令
    老板对我说,你要是能找出公司里摸鱼的人,我就给你涨薪!于是我写了两个脚本……
    Linux 命令行下搜索工具大盘点,效率提高不止一倍!
    饿了么总监分享:我是如何完成从程序员到管理层的蜕变?
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312769.html
Copyright © 2011-2022 走看看