标签:code produces appear mit write 剪枝 strlen str least
Problem UVA140-Bandwidth
Given a graph (V, E) where V is a set of nodes and E is a set of arcs in V ×V , 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 graph on the right: 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 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 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.
A B C F G D H E -> 3
题解:回溯法,剪枝主要有两点:最优化剪枝,这是显然的;如果搜索到u节点,此时u节点的孩子节点还有m个没有确定,那么最理想的情况下带宽也至少是m,因此如果m > 当前最小带宽,就完全可以剪枝。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 9 const int maxl = 200+10; 10 const int kind = 26; 11 char str[maxl]; 12 int n,Min = INF,id[256]; 13 int res[maxl]; 14 char converse[kind]; 15 bool vis[kind]; 16 vector< vector<int> > child(kind); 17 18 int pos_find(const int *num,int len,int tar){ 19 for(int k = 0;k < len;k++){ 20 if(num[k] == tar) return k; 21 } 22 return -1; 23 } 24 25 bool check(const int *ans,int &wide){ 26 int u,v; 27 for(int i = 0;i < n;i++){ 28 u = ans[i]; 29 for(int j = 0;j < child[u].size();j++){ 30 v = child[u][j]; 31 int pos = pos_find(ans,n,v); 32 wide = max(wide,abs(i-pos)); 33 if(wide > Min) return false; 34 } 35 } 36 return true; 37 } 38 39 void dfs(int *ans,int cur,int wide){ 40 //printf("cur:%d\n",cur); 41 if(wide > Min) return; 42 if(cur == n){ 43 if(check(ans,wide)){ 44 if(wide < Min){ 45 Min = wide; 46 memcpy(res,ans,n*sizeof(int)); 47 } 48 else if(wide == Min){ 49 if(res[0] == -1) memcpy(res,ans,n*sizeof(int)); 50 else{ 51 int p = 0; 52 while(res[p] == ans[p]) p++; 53 if(p!=n && ans[p]<res[p]) memcpy(res,ans,n*sizeof(int)); 54 } 55 } 56 } 57 return; 58 } 59 for(int u = 0;u < n;u++){ 60 if(!vis[u]){ 61 int cnt = 0,tmp = wide; 62 bool ok = true; 63 for(int j = 0;j < child[u].size();j++){ 64 int v = child[u][j]; 65 if(!vis[v]) continue; 66 cnt++; 67 int pos = pos_find(ans,cur,v); 68 tmp = max(wide,cur-pos); 69 //printf("tmp:%d\n",tmp); 70 if(tmp > Min){ 71 ok = false; 72 break; 73 } 74 } 75 if(!ok) continue; 76 cnt = child[u].size()-cnt; 77 if(cnt > Min) continue; 78 ans[cur] = u; 79 vis[u] = true; 80 dfs(ans,cur+1,tmp); 81 vis[u] = false; 82 } 83 } 84 } 85 86 int main() 87 { 88 //freopen("input.txt","r",stdin); 89 //freopen("output.txt","w",stdout); 90 while(~scanf("%s",str) && str[0]!=‘#‘){ 91 n = 0; 92 Min = INF; 93 memset(res,-1,sizeof(res)); 94 memset(vis,false,sizeof(vis)); 95 int len = strlen(str); 96 for(char ch = ‘A‘;ch <= ‘Z‘;ch++){ 97 if(strchr(str,ch) != NULL){ 98 id[ch-‘\0‘] = n++; 99 converse[n-1] = ch; 100 } 101 } 102 for(int i = 0;i < n;i++) child[i].clear(); 103 for(int i = 0;i < len;i++){ 104 int u = id[str[i]-‘\0‘]; 105 i += 2; 106 while(str[i]!=‘;‘ && i<len){ 107 int v = id[str[i++]-‘\0‘]; 108 child[u].push_back(v); 109 child[v].push_back(u); 110 } 111 } 112 //for(int i = 0;i < n;i++) printf("%d\n",child[i].size()); 113 int ans[kind]; 114 memset(ans,0,sizeof(ans)); 115 dfs(ans,0,0); 116 for(int i = 0;i < n;i++){ 117 printf("%c ",converse[res[i]]); 118 } 119 printf("-> %d\n",Min); 120 } 121 return 0; 122 }
标签:code produces appear mit write 剪枝 strlen str least
原文地址:https://www.cnblogs.com/npugen/p/9537634.html