Link: http://poj.org/problem? Id = 1789
A: The truck company has a long history. Each type of truck has a unique string to indicate that the length is 7. All of its trucks (except the first one) they are derived from previous trucks. Today, some bricks want to study the history of these trucks. They define the distance of the truck type encoding: the number of truck encoding strings (7 in length) with different characters at the same position. For example, if a truck code is aaaaaaa and bbaaaaa, the distance between them is 2 ,. They also defined the advantages and disadvantages of the derived scheme: 1/Σ (to, TD) D (to, TD ).
Where t0 is the base type, TD is the derived type, and D (T0, TD) is the distance between two types of truck encoding.
Now we provide the truck code to find a derived scheme with the highest advantage and disadvantage.
This question is very difficult and can be converted into a minimal spanning tree.
To maximize the merits and demerits, the denominator of the formula should be the smallest. Assume that the encoding of each truck is interpreted as the vertex of an undirected graph, and the distance between the codes of different trucks is interpreted as the weight of the undirected edge, the denominator is the weight of the Minimum Spanning Tree. In this way, the problem can be converted.
#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 2010#define eps 1e-7#define INF 0x7FFFFFFF#define seed 131#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int edge[MAXN][MAXN],vis[MAXN],dist[MAXN];int n,m,ans;char st[MAXN][10];void prim(){ int i,j; memset(vis,0,sizeof(vis)); vis[1] = 1; for(i=1;i<=n;i++) dist[i] = edge[1][i]; for(i=0;i<n-1;i++){ int temp = INF,k = -1; for(j=1;j<=n;j++){ if(!vis[j]&&dist[j]<temp){ temp = dist[j]; k = j; } } if(k==-1) break; vis[k] = 1; ans += dist[k]; for(j=1;j<=n;j++){ if(!vis[j]&&edge[k][j]<dist[j]) dist[j] = edge[k][j]; } }}int main(){ int i,j; while(scanf("%d",&n),n){ ans = 0; for(i=0;i<n;i++){ scanf("%s",st[i]); } for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ int x = 0; for(int ii=0;ii<7;ii++){ if(st[i][ii]!=st[j][ii]) x++; } edge[i+1][j+1] = edge[j+1][i+1] = x; } edge[i+1][i+1] = 0; } prim(); printf("The highest possible quality is 1/%d.\n",ans); } return 0;}
Poj1789 & amp; zoj2158 -- truck history [Minimum Spanning Tree deformation]