UVA - 140 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 v is 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



題意:

對從小排到大的,每一段字符串求出其中寬帶的最大值;然後在這些字符串中,取最小值,並且輸出該字符串;

<pre name="code" class="cpp">#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define N 100
using namespace std;

int a[N], b[N];
int endge[N][N], vis[N];
int ant;

void fun() {
	int Max, Min = 100, ans;
	do {
		Max = 0;
		for (int i = 0; i < ant; i++) 
			for (int j = i+1; j < ant; j++) {
				if (endge[a[i]][a[j]])
					ans = j-i;
				Max = max(ans,Max);
			}

		if (Min >  Max) {
			for (int i = 0;i < ant; i++)  // 進行復制,因爲a的數組一直在排序,必須要保留此時的最小值的排序
				b[i] = a[i];
			Min = Max;
		}
	} while (next_permutation(a,a+ant));

	for (int i = 0; i < ant; i++)
		printf("%c ",b[i]+'A');
	printf("-> %d\n",Min);
}

int main () {
	char str[100] ; char begin;
	while (gets(str)) {
		memset(endge,0,sizeof(endge));
		memset(vis,0,sizeof(vis));

		if (!strcmp(str,"#"))break;
		int len = strlen(str);
		for (int i = 0; i < len; i++) {
			if (str[i] == ':' || str[i] == ';')	 continue;
			else if (str[i+1] == ':')  {
				begin = str[i];	
			} else {
				endge[begin-'A'][str[i]-'A'] = 1;
				endge[str[i]-'A'][begin-'A'] = 1;
			} 

			if (isalpha(str[i]) && !vis[str[i]-'A']) 
				vis[str[i] -'A'] = 1; 
		}
		memset(a,0,sizeof(a)); ant = 0;
		for (int i = 0; i < 26; i++) if (vis[i]) 
			a[ant++] = i; // 從小到大進行排序
		fun();	
	}
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章