140 - Bandwidth
Time limit: 3.000 seconds
140 - Bandwidth
Time limit: 3.000 secondsGiven 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 vand 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
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define MAXN 50 using namespace std; int edge[MAXN], order[MAXN], minOrder[MAXN]; int G[MAXN][MAXN],loc[MAXN], minVal, cnt; char str[20000]; bool vis[50], occur[30]; void read_in(){ memset(occur, 0, sizeof(occur)); memset(G, 0, sizeof(G)); int len = strlen(str); str[len] = ';'; str[len+1] = '\0'; int i=0; while(i<(int)strlen(str)){ int u = str[i] - 'A'; occur[u] = true; ++i; ++i; while(str[i]!=';'){ int v = str[i]-'A'; occur[v] = true; ++G[u][v]; ++i; } ++i; } cnt = 0; for(int i=0; i<28; ++i) if(occur[i]){ edge[cnt++] = i; } } int counter(){ int totalMax = -999; for(int i=0; i<cnt; ++i){ int maxVal = -999; for(int j=0; j<cnt; ++j)if(G[edge[order[i]]][edge[j]]){ int pos = loc[edge[j]]; if(abs(pos-i) > maxVal) maxVal=abs(pos-i); } if(maxVal > totalMax) totalMax = maxVal; } return totalMax; } void search(int cur){ if(minVal==1) return; for(int i=0; i<cnt; ++i)if(!vis[i]){ vis[i] = true; order[cur] = i; loc[edge[i]] = cur; search(cur+1); vis[i] = false; } if(cur==cnt){ int totalMax = counter(); if(totalMax < minVal) { minVal = totalMax; memcpy(minOrder, order, sizeof(order)); } } } int main(){ #ifdef LOCAL freopen("input.txt","r",stdin); #endif while(gets(str)){ if(str[0]=='#') break; read_in(); minVal = 9999999; memset(vis, 0, sizeof(vis)); search(0); for(int i=0; i<cnt; ++i) printf("%c ",edge[minOrder[i]]+'A'); printf("-> %d\n", minVal); } return 0; }