UVA Bandwidth

题目如下:

Bandwidth 

Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in theordering between v and any node to which it is connected in thegraph. The bandwidth of the ordering is then defined as the maximum ofthe 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 minimisesthe bandwidth.

Input

Input will consist of a series of graphs. Each graph will appear on aline by itself. The entire file will be terminated by a lineconsisting of a single #. For each graph, the input will consist ofa series of records separated by `;'. Each record will consist of anode 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 willcontain no more than 8 nodes.

Output

Output will consist of one line for each graph, listing the orderingof the nodes followed by an arrow (->) and the bandwidth for thatordering. All items must be separated from their neighbours by exactlyone space. If more than one ordering produces the same bandwidth, thenchoose the smallest in lexicographic ordering, that is the one thatwould 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


给一个图,求出使图的带宽为最小值的节点的排列,带宽指每个节点的与相邻节点在排列中距离的最大值,图的带宽是节点带宽的最大值。由于节点最多只有八个,直接枚举全排列即可,如果节点数较多应该剪枝,但本题显然不需要。

AC的代码如下:

#include 
   
   
    
    

using namespace std;
int G[30][30];
int main()
{
    char s[200];
    while(cin>>s)
    {
        if(s[0]=='#')
            break;

        int length=strlen(s);
        char temp[10][20];
        int m=0,j=0;
        for(int i=0; i
    
    
     
     i)
                            MAX=max(MAX,j-i);
                        else
                            MAX=max(MAX,i-j);

                    }
                _MAX=max(_MAX,MAX);
            }
            if(_MAX
     
     
      
       "<
      
      
       
       <
       
       
      
      
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值