Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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:
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<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; const int INF=10000000; vector<int> n[10]; int ans[10],tmp[10]; int vis[10],cnt,id[256],letter[10],bw; void map_id_letter(char *str){ for(int c='A';c<='Z';c++) if(strchr(str,c)){ id[c]=cnt; letter[cnt]=c; cnt++; } } void dfs1(int cur,int mbw){ //printf("%d%d",cur,mbw); if(cur==cnt) { bw=mbw; // printf("1"); for(int i=0;i<cnt;i++) ans[i]=tmp[i]; } else for(int i=0;i<cnt;i++){ //printf("%d",i); if(!vis[i]){ tmp[cur]=i; //printf("%d",tmp[cur]); for(int j=0;j<cur;j++){ int tag=0; for(int k=0;k<n[i].size();k++) if(n[i][k]==tmp[j]){ mbw=max(cur-j,mbw); //printf("%d",mbw); tag=1; break; } if(tag) break; } if(mbw<bw){ vis[i]=1; dfs1(cur+1,mbw); vis[i]=0; } } } } int main(){ char buff[100]; //freopen("./uva140.in.txt","r",stdin); while(scanf("%s",buff)&&buff[0]!='#'){ for(int i=0;i<10;i++) n[i].clear(); cnt=0; map_id_letter(buff); char* p=strtok(buff,";"); while(p){ int idn=id[*p]; ++p; while(*(++p)){ n[idn].push_back(id[*p]); n[id[*p]].push_back(idn); } p=strtok(NULL,";"); } bw=INF; dfs1(0,0); //printf("%d ",bw); for(int i=0;i<cnt;i++) printf("%c ",letter[ans[i]]); printf("-> %d\n",bw); //for(int j=0;j<cnt;j++) // for(int i=0;i<n[j].size();i++) printf("%d ",n[j][i]); } return 0; }