Topic Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_ problem&problem=76
Type: Backtracking
Original title:
Given a graph (v,e) where V is a set of nodes and E are a set of arcs in VXV, and "ordering on" elements in V, then th Ebandwidth 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. For example, consider the following graph:
This can is ordered in many ways, two of which are illustrated below:
For this orderings, the bandwidths of the nodes (in order) are 6, 6, 1, 4, 1, 1, 6, 6 giving a ordering bandwidth of 6, and 5, 3, 1, 4, 3, 5, 1, 4 giving an ordering bandwidth of 5.
Write a program that'll find the ordering of a graph that minimises the bandwidth.
The main effect of the topic:
Give a picture, arrange each node, then for each point in the arrangement, find the position in the arrangement of the points connected to it in the graph, calculate the maximum distance between them in the arrangement, and finally, take the maximum distance of the permutation to the bandwidth.
The title asks us to find the permutation of the smallest bandwidth in all permutations, and output this permutation and this bandwidth
Analysis and Summary:
A relatively simple retrospective problem, slightly troublesome is the input of the topic.
Only need to find out all the bandwidth, take the smallest one. Note that there may be multiple scenarios where the problem requires that the output be sorted by the smallest dictionary order. As long as the nodes are arranged sequentially, the first answer to the recursive backtracking must be the one with the smallest dictionary order.
Code:
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define MAXN u
Sing 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] = ' the ';
int i=0;
int OUT[MAXN], top=0;
while (I<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;
an 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);
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; }