Bandwidth

全排列解法

Given a graph (V,E)(V,E) where VV is a set of nodes and EE is a set of arcs in V×VV×V , and an ordering on the elements in VV , then the bandwidth of a node vv is defined as the maximum distance in the ordering between vv 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 graph on the right:

83fd44131c7cd6b901784c5e6129670d.png

This can be ordered in many ways, two of which are illustrated below:

094d8a6adb84ba8ca02f6a9662129d0f.png

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.

给定V中元素的一种排序,那么顶点v的带宽定义如下:在当前给定的排序中,与v距离最远的且与v有边相连的顶点与v的距离。给定排序的带宽定义为各顶点带宽的最大值。

输入描述:

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

样例:

输入 

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

输出 

A B C F G D H E -> 3
#include<bits/stdc++.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
typedef long long ll;
//The graph will contain no more than 8 nodes.
//直接全排列
string str;
int main()
{
	int f,t;
	int a[50][50];
	while(cin>>str){
		if(str[0]=='#')
			return 0;
		int h[50]={0},node[50]={0},goal[50]={0},cnt=0;
		memset(a,0,sizeof(a));
		f=0;
		int len=str.length();
		for(int i=0;i<len;i++){
			if(str[i]>='A'&&str[i]<='Z'){
				h[str[i]-'A']=1;
				if(f==0)	t=str[i]-'A';
				else a[t][str[i]-'A']=a[str[i]-'A'][t]=1;
			}
			else if(str[i]==':')f=1;
			else if(str[i]==';')f=0;
			
		}
		int mm=100000;
		for(int i=0;i<26;i++){
			if(h[i]==1)
				node[cnt++]=i;//个数 
		}
		do{
            int sum=0;
            for(int i=0;i<cnt;i++)
                for(int j=i+1;j<cnt;j++)
                    if(a[node[i]][node[j]])
                        if(abs(i-j)>sum)
                            sum=abs(i-j);
            if(sum<mm)
            {
                mm=sum;
                memcpy(goal,node,sizeof(node));//
            }
 
        }while(next_permutation(node,node+cnt));//函数 
		for(int i=0;i<cnt;i++){
			printf("%c ",goal[i]+'A');
		}
		cout<<"-> "<<mm<<endl;
	}
	
	return 0;
 } 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值