UVA 140 Bandwidth (带宽)

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的距离。给定排序的带宽定义为各顶点带宽的最大值。例如考虑如下图:

 

140img1

 

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

 

140img2

 

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


译文链接:http://www.cnblogs.com/devymex/p/3264538.html


【分析】

         此题判断n个点连线最长的练线的所有序列中最短的序列。

        2种解法:

        1. 用全排列枚举所有序列,求出每个序列中最长的连线,与当前最短连线序列进行比较,如果此连线小于

   最短连线序列,就更新最短连线序列。依次遍历完所有排列,本题最多8个点,所有不会超时。

        2. 通过递归回溯的方法,枚举可能的解,中间如果连线比已知的最短序列长度还长就不进行递归,这样可

   以剪掉很多无用的递归。但是这个题说点用26个字母表示,所有给的点不一定是A、B、C、D、E、F、G……

   点可能是不连续的,可能不是从A开始的,需要进行数组保存再简单映射,不然没有办法判断点之间的连接关

   系(错了好几次)。


【代码】


方法一:全排列

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int e[27][27];

int main()
{
    char s[100];
    while(scanf("%s",s)&&s[0]!='#')
    {
        memset(e,0,sizeof(e));
        int flag=0,len=strlen(s),ans;
        int book[27]={0};
        for(int i=0;i<len;i++)
        {
            if(s[i]>='A'&&s[i]<='Z')
            {
                book[s[i]-'A']=1;
                if(flag==0)
                    ans=s[i]-'A';
                else
                    e[ans][s[i]-'A']=e[s[i]-'A'][ans]=1;
            }
            else if(s[i]==':') flag=1;
            else if(s[i]==';') flag=0;
        }
        int num=0,Max=INF,node[27]={0},goal[27]={0};
        for(int i=0;i<26;i++)
            if(book[i])
                node[num++]=i;
        do
        {
            int sum=0;
            for(int i=0;i<num;i++)
                for(int j=i+1;j<num;j++)
                    if(e[node[i]][node[j]])
                        if(abs(i-j)>sum)
                            sum=abs(i-j);
            if(sum<Max)
            {
                Max=sum;
                memcpy(goal,node,sizeof(node));
            }

        }while(next_permutation(node,node+num));
        for(int i=0;i<num;i++)
            printf("%c ",goal[i]+'A');
        printf("-> %d\n",Max);
    }
    return 0;
}


方法二:递归回溯

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int e[27][27],book[10],goal[10],node[10],letter[10],num,Min;

void init()
{
    num=0;
    Min=10;
    for(int i=0;i<10;i++)
        book[i]=goal[i]=node[i]=letter[10]=0;
    memset(e,0,sizeof(e));
    return ;
}

void dfs(int n,int Max)
{
    if(n==num)
    {
        Min=Max;
        memcpy(goal,node,sizeof(node));
        return ;
    }
    for(int i=0;i<num;i++)
    {
        if(book[i]==0)
        {
            book[i]=1;
            node[n]=letter[i];  // 保存成字母表中第几个字母,如果只保存i的话,就没办法判断连接关系
            int w=0;
            for(int j=0;j<n;j++)
                if(e[letter[i]][node[j]])  /* 注意 */// 判断第n个点与前面点是否连接
                {
                    w=n-j;
                    break;
                }
            int Max_w=max(Max,w);
            if(Max_w<Min)
                dfs(n+1,Max_w);
            book[i]=0;
        }
    }
    return ;
}

int main()
{
    char s[100];
    while(scanf("%s",s)&&s[0]!='#')
    {
        init();
        int len=strlen(s),flag=0,ans;
        int al[27]={0};
        for(int i=0;i<len;i++)
        {
            if(s[i]>='A'&&s[i]<='Z')
            {
                al[s[i]-'A']=1;
                if(flag==0)
                    ans=s[i]-'A';
                else e[ans][s[i]-'A']=e[s[i]-'A'][ans]=1;
            }
            else if(s[i]==':') flag=1;
            else if(s[i]==';') flag=0;
        }
        for(int i=0;i<26;i++)   // 输入的字母可能不是从‘A’开始或者连续的,所以需要保存下来有哪些点出现过
            if(al[i])
                letter[num++]=i;
        dfs(0,0);
        for(int i=0;i<num;i++)
            printf("%c ",goal[i]+'A');
        printf("-> %d\n",Min);
    }
    return 0;
}

【收获】

                    刚开始做暴力题,第一次用到全排列,感觉全排列还是很有用的,之前没用过全排列,感觉用不到耗时

      挺多,现在看来有些题在不超时的情况下用全排列还是很节约代码长度。

               此题用递归回溯来做是一个不错的考验,考虑到很多细节和递归回溯,看到别的博客写的先遍历输入的

      字符串的字母对齐映射、保存,再进行遍历连接,连接时就可以起到从0~n之间的连接,也是一种不错的

      法,值得学习。

               strtok函数的应用,虽然我写的代码里没有用到strtok函数但是看到别人用到strtok函数,才知道这个函

      数,这个函数起到以给定分离符分离字符串的作用,感觉还是挺有用的,这个题用此函数自我感觉比较麻烦

      不适合菜鸟理解(我也是个菜鸟,看了好久,百度了很多地方才懂)。


【扩展】

                 此题不错的博客:http://www.cnblogs.com/cute/p/3806164.html

                 strtok不错的讲解:http://www.cnblogs.com/lixiaohui-ambition/archive/2012/07/18/2598042.html




猜你喜欢

转载自blog.csdn.net/disparity_cjk/article/details/52963350