Bandwidth



 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

题意:全排列。题中给出每一个点相邻的点;A相邻得点就是F、B............然后每一次全排列后。找出相邻点的距离,选出一个最大值;  然后在这些最大值中找出一个最小值;
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
char str[1000];
while(scanf("%s",str)!=EOF)
{
  if(str[0]=='#')break;
  int n=0,id[256];
  char s[1000];
   for(char ch='A';ch<='Z';ch++)
   {
     if(strchr(str,ch)!=NULL)
      {
        id[ch]=n++;
        s[id[ch]]=ch;               
      }     
   }
   int p=0,q=0,i,j,l=strlen(str);
   vector<int> u,v;
   while(1)
   {
     while(p<l&&str[p]!=':')p++;
     if(p==l) break;
     while(q<l&&str[q]!=';') q++;
     for(i=p+1;i<q;i++)
     {
      u.push_back(id[str[p-1]]);
      v.push_back(id[str[i]]);                
     }
       p++;q++;
   }
     int M[10],N[10],best[10],ans=n;
      for(i=0;i<n;i++) M[i]=i;
      do{
         for(i=0;i<n;i++)  N[M[i]]=i;//每排一次序,N[]要重新编号
               int banwidth=0;     
           for(i=0;i<u.size();i++)    
              banwidth=max(banwidth,abs(N[u[i]]-N[v[i]]));
           if(banwidth<ans)
           {
             ans=banwidth;
             memcpy(best,M,sizeof(M));               
           }
       }while(next_permutation(M, M+n));//全排列
      for(i=0;i<n;i++)
       printf("%c ",s[best[i]]);
       printf("-> %d\n",ans);
}    
return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值