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:

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

题意:。。。又是一道坑英文的题目。。。题目很绕。根据输入 比如A:FB; 代表A和F 和B连接。。要求这个字母串经过各种排列后。生成的每个串中。所有连接的点的距离中。最大得距离作为带宽。然后在所有串中找出最小的带宽。。

思路:全排列每种宽度算出来做比较就可以。。字符串的处理上也要注意

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

char sb;
char a;
int map[205][205];
int so[8];
int zi[27];
int out[8];
int zinum = 0;
int judge = 0;
int minn = 999999999;

int findmax()
{
    int maxx = 0;
    for (int i = 0; i < zinum; i ++)
	for (int j = i + 1; j < zinum; j ++)
	{
	    if (map[so[i]][so[j]] && maxx < j - i)
	    {
		maxx = j - i;
	    }
	}
    return maxx;
}
int main()
{
    while ((sb = getchar()) != EOF)
    {
	if (sb == '#')
	    break;
	if (sb == '\n')
	{
	    for (int i = 0; i < 27; i ++)
	    {
		if (zi[i] == 1)
		{
		    so[zinum ++] = i;
		}
	    }
	    sort(so, so + zinum);
	    if (minn > findmax())
	    {
		minn = findmax();
		for (int i = 0; i < zinum; i ++)
		    out[i] = so[i];
	    }
	    while (next_permutation(so, so + zinum))
	    {
		if (minn > findmax())
		{
		    minn = findmax();
		    for (int i = 0; i < zinum; i ++)
			out[i] = so[i];
		}
	    }
	    for (int i = 0; i < zinum; i ++)
		printf("%c ", out[i] + 'A');
	    printf("-> %d\n", minn);
	    judge = 0;
	    zinum = 0;
	    minn = 999999999;
	    memset(zi, 0, sizeof(zi));
	    memset(so, 0, sizeof(so));
	    memset(out, 0, sizeof(out));
	    memset(map, 0, sizeof(map));
	}
	if (sb == ':')
	{
	    judge = 1;
	    continue;
	}
	if (sb == ';')
	{
	    judge = 0;
	    continue;
	}
	if (judge == 0)
	{
	    a = sb;
	    if (zi[sb - 'A'] == 0)
	    {
		zi[sb - 'A'] = 1;
	    }
	    continue;
	}
	if (judge == 1)
	{
	    map[a - 'A'][sb - 'A'] = map[sb - 'A'][a - 'A'] = 1;
	    if (zi[sb - 'A'] == 0)
	    {
		zi[sb - 'A'] = 1;
	    }
	}
    }
    return 0;
}


你可能感兴趣的:(UVA 140 Bandwidth)