UVa 140 Bandwidth

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题目还要多写,不然思路还是不透彻(回溯+剪枝优化)


发布了59 篇原创文章 · 获赞 8 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章