uva 140 Bandwidth (全排列+暴力枚举)

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

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

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

题目大意:给出一些点,以及所有必须相连的两点,然后每个排序中,找出必须连接的距离最大值,然后在所有序列中找出连接所需最短的。

解题思路:先将节点关系构成一张表,然后根据表进行全排列进行枚举(利用next_permutation函数),找出最小带宽。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
char ch[10], ch2[10];
int A[30][30], Max, Min, a[26];
int find(char a) {
	for (int i = 0; i < 10; i++) {
		if (ch[i] == a) return i;
	}
}
void getMin() { //找出给排列情况的带宽
	int temp1, temp2, num;
	for (int i = 0; i < 26; i++) {
		for (int j = 0; j < 26; j++) {
			if (A[i][j]) {
				temp1 = find(i + 'A');
				temp2 = find(j + 'A');
				num = abs(temp1 - temp2);
				if (Max < num) {
					Max = num;
				}
			}
		}
	}
}
int main() {
	char str[100];
	while (scanf("%s", str) == 1 && strcmp(str, "#") != 0) {
		memset(A, 0, sizeof(A));
		memset(a, 0, sizeof(a));
		int len = strlen(str);
		int cnt1, cnt2 = 0, flag = 1;
		for (int i = 0; i < len; i++) { //构成一张关系表
			if (str[i] >= 'A' && str[i] <= 'Z') {
				a[str[i] - 'A']++;
				if (flag) {
					cnt1 = str[i] - 'A';
				}
				else {
					A[cnt1][str[i] - 'A'] = 1;
				}
			}
			else if (str[i] == ':') flag = 0;
			else if (str[i] == ';') flag = 1;
		}
		memset(ch, 0, sizeof(ch));
		memset(ch2, 0, sizeof(ch2));
		for (int i = 0; i < 26; i++) {//找出出现过的节点字母编号
			if (a[i] != 0) ch[cnt2++] = i + 'A';
		}
		Min = 10;
		sort(ch, ch + strlen(ch));//排序,为全排列做准备
		do{                         //全排列找出最小带宽
			Max = 0;
			getMin();
			if (Min > Max) {
				strcpy(ch2, ch);
				Min = Max;
			}
		}
		while (next_permutation(ch, ch + strlen(ch)));
		for (int i = 0; i < strlen(ch2); i++) {
			printf("%c ", ch2[i]);
		}
		printf("-> %d\n", Min);
	}
	return 0;
}
时间: 2024-08-05 06:20:12

uva 140 Bandwidth (全排列+暴力枚举)的相关文章

uva 140 Bandwidth(全排列+递归)

快睡觉的时候1A的把序列全排列,递归暴力判断就ok啦,我改成对应的整数存了,a数组存的是所有的字符的排列, b数组存的是所有开始节点的排列,map[i][j]数组存的是i为起点,与j相邻 贴代码: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<limits.h> #include<math.h> #include<algorithm> using na

[2016-02-20][UVA][140][Bandwidth]

UVA - 140 Bandwidth Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description 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 

uva 565 - Pizza Anyone?(暴力枚举 + 二进制)

题目:uva 565 - Pizza Anyone?(暴力枚举 + 二进制) 题目大意:题目是说有一个人要帮他的朋友们定批萨,然后每个朋友都有自己的口味要求,问能不能定一个批萨然后满足每个朋友的至少一个要求. 能就输出所定批萨里面加的东西,,输出要求按字典序: 不能就输出:No pizza can satisfy these requests. 解题思路:这题里面有16种材料,每种材料只有取与不取的可能,这样就有 2^16 种( 0 - 2^16 - 1),枚举出每种情况然后在分别看是否能满足每

uva 725 Division(暴力枚举)

uva 725  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij =

UVA - 140 Bandwidth(带宽)(全排列)

题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #i

uva 11210 Chinese Mahjong(暴力枚举)

uva 11210 Chinese Mahjong Mahjong () is a game of Chinese origin usually played by four persons with tiles resembling dominoes and bearing various designs, which are drawn and discarded until one player wins with a hand of four combinations of three

UVa 140 Bandwidth(DFS 回溯 剪枝)

题意  有一个无向图  对于其所有顶点的一个排列 称一顶点与所有与其有边的其它顶点在排列中下标差的最大值为这个顶点的带宽   而所有顶点带宽的最大值为这个排列的带宽   求这个图带宽最小的排列 枚举排列  ans记录当前找到的最小带宽  枚举过程中一旦出现带宽大于ans的也就不用再扩展了  这样枚举完就得到了答案 #include<cstdio> #include<cstring> using namespace std; const int N = 50; int n, ans,

uva 140 bandwidth (好题) ——yhx

 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 con

UVa 10603 Fill [暴力枚举、路径搜索]

10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The rst and the second jug are initially empty, while the third is completely lled with water. It is allowed to pour water f