UV 140 Bandwidth (full arrangement + brute force enumeration)
UV 140 Bandwidth
Given a graph (V, E) where V is a set of nodes and E is a set of arcs in VxV, andOrderingOn the elements in V, thenBandwidthOf a nodeVIs defined as the maximum distance in the orderingVAnd 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 several ways, two of which are using strated 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 ';'. each record will consist of a node name (a single upper case character in the range 'A' to 'Z'), followed by ': and at least one of its neighbors. 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 neighbors 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 wowould 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
Give some points and all the two points that must be connected. Then, in each sorting, find the maximum distance that must be connected, and then find the shortest required for the connection in all sequences.
Solution: first form a table of node relationships, and then enumerate the tables in full order (using the next_permutation function) to find the minimum bandwidth.
# Include
# Include
# Include
Using namespace std; char ch [10], ch2 [10]; int A [30] [30], Max, Min, a [26]; int find (char) {for (int I = 0; I <10; I ++) {if (ch [I] = a) return I ;}} void getMin () {// find the bandwidth 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]; whil E (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 ++) {// create A relational table 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 ++) {// locate the occurrence node letter number if (a [I]! = 0) ch [cnt2 ++] = I + 'a';} Min = 10; sort (ch, ch + strlen (ch); // sort, prepare do {// for full sorting to find the minimum bandwidth 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 ;}