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

    解題報告:枚舉所有排列,計算出最小值。沒啥好說的。代碼如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM
    work();
}

/***************************************************/

char str[10];
char tmp[10];
bool has[100];
bool line[100][100];

void work()
{
    char ch;
    while(ch = getchar(), ch != '#')
    {
        memset(has, 0, sizeof(has));
        memset(line, 0, sizeof(line));

        int status = 0;
        char cur;
        do
        {
            if(ch == ':')
                status = 1;
            else if(ch == ';')
                status = 0;
            else if(status == 0)
                cur = ch, has[ch] = true;
            else if(status == 1)
                line[cur][ch] = line[ch][cur] = true, has[ch] = true;
        } while(ch = getchar(), ch != '\n');

        int len = 0;
        fff(i, 'A', 'Z') if(has[i])
            str[len++] = i;

        int ans = 10;
        do
        {
            int maxD = 0;
            ff(i, len) ff(j, len) if(line[str[i]][str[j]])
                maxD = max(maxD, abs(i-j));

            if(maxD < ans)
            {
                ans = maxD;
                memcpy(tmp, str, sizeof(str));
            }
        } while(next_permutation(str, str+len));

        ff(i, len) printf("%c ", tmp[i]);
        printf("-> %d\n", ans);
    }
}
發佈了227 篇原創文章 · 獲贊 13 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章