Description
Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company’s history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.
Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan – i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as
1/Σ(to,td)d(to,td)
where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) is the distance of the types.
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.
Input
The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.
Output
For each test case, your program should output the text “The highest possible quality is 1/Q.”, where 1/Q is the quality of the best derivation plan.
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
题意:题目上说的是ACM公司原来只有一种类型的卡车,后来派生出来其他类型的卡车,每种类型的卡车都是有7个小写字母组成,每两种卡车之间有都少位不同的字母就是这两种卡车之间的距离,这个公司要找一个推导计划,如题目上公式所示,要求你帮助他找到一个最高质量的推倒计划。
解题思路:要想使题目上的 值最大,则分母应该尽可能的小,所以只要找到一种将所有类型的卡车联系起来,且距离最小的方法就OK了,想到这就很容易想到最小生成树这个算法了,没错,这个题可以用Prim算法解决。下面要解决的就是构图问题了,我的构图方法是将每一类型卡车与其他 类型卡车的距离都算出来,然后存在Map中,最后找Map中的最小生成树。
#include <iostream>
#include <cstdio>
#define INF 0x3f3f3f3f
using namespace std;
int Map[2005][2005],sum;int n;
void Prim(int v)
{
int lowcost[2005],Min,k;//lowest用来储存没有加入到最小生成树中边的权重
for(int i=0;i<n;i++)
{
lowcost[i]=Map[v][i];
}
for(int i=1;i<n;i++)
{
Min = INF;
for(int j=0;j<n;j++)
if(lowcost[j]!=0&&lowcost[j]<Min)
{
Min = lowcost[j];k=j;
}
sum+=Min;//找到最小的边累加,
lowcost[k]=0;//标记,表示已经加入到最小生成树中
for(int j=0;j<n;j++)
{
if(Map[k][j]!=0&&Map[k][j]<lowcost[j])
{
lowcost[j]=Map[k][j];
}
}
}
}
int main()
{
while(~scanf("%d",&n),n)
{ string str[2005];
for(int i=0;i<n;i++)
cin>>str[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int w=0;//用来记录与其他字符串不一样的位数
for(int k=0;k<7;k++)
if(str[i][k]!=str[j][k])
w++;
Map[i][j]=w;
}
}
sum=0;//用来累加最小生成树中每条边上的权重
Prim(0);
printf("The highest possible quality is 1/%d.\n",sum);
}
return 0;
}