UVa 140 Bandwidth

本文探讨了图论中的一个经典问题:寻找一种节点排序方式以最小化带宽。通过定义节点带宽及整体排序带宽的概念,采用深度优先搜索结合剪枝策略实现最优解的查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and anordering on the elements in V, then thebandwidth of a node v is defined as the maximum distance in the ordering betweenv 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

#include <cstdio>  
#include <cstring>  
#include<climits>
#include<cctype>
#define INF INT_MAX  
int ve[30][30], node[30], permu[30], ans[30], _min, c;  
void analyze(char *s)  
{  
	int state = 0;  
    for(int i = 0;s[i]; i++)  
    {  
		if(isalpha(s[i]))  
        {  
			if(!node[s[i]-'A'+1])
			{
				node[s[i]-'A'+1] = 1; 
				c++;
			}  
            if(!state) 
				state = s[i]-'A'+1;  
            else //邻接矩阵表示
			{	//记录结点之间相邻属性
				ve[state][s[i]-'A'+1] = 1; 
				ve[s[i]-'A'+1][state] = 1;
			}  
        }  
        else if(s[i]==';'&&state) 
			state = 0;  
    }  
}  
void dfs(int cur, int max)  //max表示上一个结点最大带宽
{  
	if(cur==c)  
    {  
		if(_min>max) 
		{ 
			for(int i = 0; i < c; i++)  
				ans[i] = permu[i];
			_min = max; 
		}  
        return;  
    }  
    for(int i = 1; i <=26; i++) 
		if(node[i])  
		{  //计算当前结点的带宽有没有比上一个结点带宽大的情况
			for(int j = cur-max-1; j >= 0; j--) //计算cur这个结点最大带宽
				if(ve[i][permu[j]]) //判断是不是邻接关系,当前结点的最大带宽肯定大于上一个结点的最大带宽
					max = cur-j;

			if(max>_min) //剪枝
				return;

			node[i] = 0; 
			permu[cur] = i; 
			dfs(cur+1, max); 
			node[i] = 1;  
		}  
}  
int main ()  
{  
	char s[100];  
    while(scanf("%s",s)&&s[0]!='#')  
    {  
        memset(node,0,sizeof(node));  
        memset(ve,0,sizeof(ve));  
        c = 0;  
        analyze(s);  
        _min = INF;  
        for(int i = 1; i <= 26; i++) //图G所有结点排列
			if(node[i])  
			{ 
				node[i] = 0; //标记此结点已经访问过
				permu[0] = i; //一个全排列第一位字母
				dfs(1,0); 
				node[i] = 1;
			}  
        for(int i = 0; i < c; i++) 
			printf("%c ", ans[i]+'A'-1);  
        printf("-> %d\n",_min);  
    }  
    return 0;  
}


DFS题目还要多写,不然思路还是不透彻(回溯+剪枝优化)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值