yooocen / dadaLearningBlogs

入职之后所有的学习文档
0 stars 0 forks source link

uva:140 Bandwidth (带宽) #26

Open yooocen opened 6 years ago

yooocen commented 6 years ago

Problem 问题 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: 给定一个图(V,E),其中V为顶点的集合,E为边的集合,属于VxV。给定V中元素的一种排序,那么顶点v的带宽定义如下:在当前给定的排序中,与v距离最远的且与v有边相连的顶点与v的距离。给定排序的带宽定义为各顶点带宽的最大值。例如考虑如下图:

This can be ordered in many ways, two of which are illustrated below: 此图可以给出多种排序,其中两个排序图示如下:

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. 对于给出的这两个排序,它们各结点的带宽分别是(按排序顺序):6, 6, 1, 4, 1, 1, 6, 6,排序带宽为6,以及5, 3, 1, 4, 3, 5, 1, 4,排序带宽为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 neighbors. The graph will contain no more than 8 nodes. 输入由一系列的图构成。每个图独占一行。一个仅包含“#”字符的一行输入标志整个输入文件结束。对于每个图的输入,都包含一系列由“;”隔开的记录。每个记录包含一个结点名(一个大写字母,范围是“A”到“Z”),接着是一个“:”,然后是一些该结点的邻居结点。图中不会包含超过8个结点。

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. 每个图对应一行输出,列出排序的结点,然后是一个箭头(->)以及该排序的带宽值。所有项都应由一个空格与它相邻的项隔开。如果同一个带宽有多种排序方法,取字母序最小的一种排序,也就是取字母表排在前面的一种排序。

【分析】

  1. 用深度搜索生成全排列,其实就是根据字母集做一个全排列,暴力看看哪一个符合而已,中间可以用剪枝的方法减少递归次数,使用permeation可以生成全排列,见注释的代码
  2. 使用的数据结构是set、map、二维数组存图的相关的信息,set就是字典,无重复并且可以排序(c++),map存字符和其索引
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<string>
using namespace std;

const int maxn = 8;
set<char> dict;//字典
map<char, int> m;
vector<char> v;
int n;
int a[maxn][maxn];
int p[maxn];
bool vis[maxn];
int bd;  //记录最优带宽
int ans[maxn];

int getid(char c){
    return m[c];
}

void init(string s){
    m.clear();
    v.clear();
    dict.clear();
    memset(a, 0, sizeof(a));
    memset(vis, 0, sizeof(vis));

    //获取所有字母
    for (int i = 0; i < s.length(); i++){
        char ch = s[i];
        dict.insert(ch);
        i = i + 2;
        while (i < s.length() && s[i] != ';'){
            ch = s[i];
            dict.insert(ch);
            i++;
        }
    }

    for (set<char>::iterator p = dict.begin(); p != dict.end(); p++){
        v.push_back(*p);
        m[*p] = v.size() - 1;
    }

    for (int i = 0; i < s.length(); i++){
        int p = getid(s[i]);
        i=i+2;
        while (i < s.length() && s[i] != ';'){
            int q = getid(s[i]);
            a[p][q] = a[q][p] = 1;
            i++;
        }
    }
    n = v.size();
    bd = 1000;
}
int abs(int x){
    return x >= 0 ? x : -x;
}

int calcbd(int x,int cur, int n){
    int max = 0;
    for (int i = 0; i < n; i++){
        int t = p[i];
        if (a[x][t] && abs(cur - i)>max)
            max = abs(cur - i);
    }
    return max;
}

void solve(int cur){
    if (cur == n){
        int tmp=0;
        //序列的最后一个字符确定后,是否会有更小的带宽
        for (int i = 0; i < n; i++){
            int t = calcbd(p[i], i, n);
            if (t>tmp)
                tmp = t;
        }
        if (tmp < bd){
            bd = tmp;
            for (int i = 0; i < n; i++)
                ans[i] = p[i];
        }
    }
    else{
        for (int i = 0; i < n; i++){
            if (!vis[i] && calcbd(i,cur,cur)<bd){
                vis[i] = true;
                p[cur] = i;
                solve(cur + 1);
                vis[i] = false;
            }
        }
    }
}

void printans(){
    for (int i = 0; i < n; i++)
        cout<< v[ans[i]]<<" ";
    cout << "-> " << bd << endl;
}

int main(){
    string s;
    while (cin >> s && s[0] != '#'){
        init(s);
        solve(0);
        /*使用next_permutation函数,若要用这个则不需要solve函数
         for (int i = 0; i < n; i++)
         p[i] = i;
         do{
         int tmp = 0;
         for (int i = 0; i < n; i++){
         int t = calcbd(p[i], i, n);
         if (t>tmp)
         tmp = t;
         }
         if (bd>tmp){
         bd = tmp;
         for (int i = 0; i < n; i++)
         ans[i] = p[i];
         }
         } while (next_permutation(p,p+n));*/
        printans();
    }
    return 0;
}