UVa - 140 - Bandwidth

来源:互联网 发布:怎么安装软件字库 编辑:程序博客网 时间:2024/05/16 14:05

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

直接枚举全排列,计算带宽,记录最小的情况,如果有一样的,取字典序小的就可以了。生成排列的时候用了STL函数next_permutation。

AC代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cctype>#include <cstring>#include <string>#include <sstream>#include <vector>#include <set>#include <map>#include <algorithm>#include <stack>#include <queue>#include <bitset> #include <cassert> #include <cmath>#include <functional>using namespace std;const int maxn = 10;int id[256], letter[maxn];char input[1000];int n;void init() // 计算结点的个数并给字母编号{n = 0;for (char ch = 'A'; ch <= 'Z'; ch++) {if (strchr(input, ch) != NULL) {id[ch] = n++;letter[id[ch]] = ch;}}}// 处理输入vector <int> u, v;void readInput(){int len = strlen(input);int p = 0, q = 0;u.clear();v.clear();while (1) {while (p < len && input[p] != ':') {p++;}if (p == len) {break;}while (q < len && input[q] != ';') {q++;}for (int i = p + 1; i < q; i++) {u.push_back(id[input[p - 1]]);v.push_back(id[input[i]]);}p++;q++;}}// 枚举全排列void solve(){int P[maxn], bestP[maxn], pos[maxn], ans = n;for (int i = 0; i < n; i++) {P[i] = i;}do {for (int i = 0; i < n; i++) {pos[P[i]] = i; // 每个字母的位置}int bandwidth = 0;for (int i = 0; i < u.size(); i++) {bandwidth = max(bandwidth, abs(pos[u[i]] - pos[v[i]])); // 计算带宽}if (bandwidth < ans) {ans = bandwidth;memcpy(bestP, P, sizeof(P));}} while (next_permutation(P, P + n));// 输出for (int i = 0; i < n; i++) {cout << (char)letter[bestP[i]] << ' ';}cout << "-> " << ans << endl;}int main(){while (cin >> input && input[0] != '#') {init();readInput();solve();}return 0;}




0 0
原创粉丝点击