Given a graph (v,e) where V is a set of nodes and E are a set of arcs in VXV, and an ordering on the elements in V , then the bandwidth of a node v are defined as the maximum distance in the ordering between v and any node to which it's 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 is ordered in many ways, and the which is illustrated below:
For these orderings, the bandwidths of the nodes (in order) is 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 would find the ordering of a graph that minimises the bandwidth.
Input
Input would consist of a series of graphs. Each graph would appear on a line by itself. The entire file would be terminated to a line consisting of a single #. For each graph, the input would consist of a series of records separated by '; '. Each record would consist of a node name (a single upper case character with the range ' a ' to ' Z '), followed by a ': ' and At least one of its neighbours. The graph would contain no more than 8 nodes.
Output
Output would consist of one line for each graph, listing the ordering of the nodes followed by an arrow and the ban Dwidth for that ordering. All items must is separated from their neighbours by exactly one space. If more than one ordering produces the same bandwidth, then choose the smallest in lexicographic ordering, which is the one That's would appear first in an alphabetic listing.
1#include <cstdio>2#include <cstring>3 BOOLuse[Ten],app[ -];4 intmap[ -][ -],n,a[Ten],ans,ans_arr[Ten],alfa[ -];5 voidDfsintFinintcur)6 {7 inti,j,k,p,q,x,y,z,cnt,temp;8 Charc1,c2;9 if(Cur>=ans)return;TenCnt=0; One for(i=1; i<=n;i++) A if(Map[alfa[a[fin]]][alfa[i]]&&!use[i]) cnt++; - if(Cnt>=ans)return; - if(fin==N) the { -ans=cur; - for(i=1; i<=n;i++) -ans_arr[i]=A[i]; + return; - } +fin++; A for(i=1; i<=n;i++) at if(!Use[i]) - { -a[fin]=i; -temp=cur; -use[i]=1; - for(j=1; j<fin-cur;j++) in if(Map[alfa[i]][alfa[a[j]]) - { totemp=fin-J; + Break; - } the DFS (fin,temp); *use[i]=0; $ }Panax Notoginseng } - intMain () the { + Chars[ +],c; A inti,x,y; the while(SCANF ("%s", s) &&s[0]!='#') + { -memset (Map,0,sizeof(map)); $memset (use,0,sizeof(use)); $Memset (A,0,sizeof(a)); -memset (Alfa,0,sizeof(Alfa)); -memset (App,0,sizeof(APP)); then=0; - for(i=0;i<strlen (s);)Wuyi { thec=s[i++]; -x=c-'A'+1; Wuapp[x]=1; -i++; About while(s[i]!=';'&&i<strlen (s)) $ { -y=s[i]-'A'+1; -app[y]=1; -map[x][y]=map[y][x]=1; Ai++; + } thei++; - } $ for(i=1; i<= -; i++) the if(App[i]) thealfa[++n]=i; theans=0x3f3f3f3f; theDfs0,0); - for(i=1; i<=n;i++) inprintf"%c", alfa[ans_arr[i]]+'A'-1); theprintf"%d\n", ans); the } About}
Search + prune.
1. Current bandwidth greater than equals known answer, pruning.
2. When a node is searched, the number of points connected to the node that are not yet joined is equal to the known answer, pruning. (If these points are all immediately following this point, the bandwidth is also the number.) otherwise larger. )
When reading data processing, be careful to keep the alphabetical order in front of the queue.
Note the variable references for various subscripts, letters, numbers, and positions.
UVA bandwidth (good title)--yhx