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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章