Truck History
|
Language:Default
Truck History
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 where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) 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. Source |
题意杀,自己百度题意去…
最小生成树的
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=4006;
int n,m,pre[MAXN];
char str[MAXN][10];
struct Edge{
int s,e,dis;
Edge(){}
Edge(int _s,int _e,int _dis)
{
s=_s; e=_e; dis=_dis;
}
};
bool cmp(Edge a,Edge b)
{
return a.dis<b.dis;
}
void init()
{
for(int i=0;i<=n;i++)
pre[i]=i;
}
int find(int x)
{
return pre[x]==x?x:pre[x]=find(pre[x]);
}
bool join(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
pre[x]=y;
return true;
}
return false;
}
int sdis(int i,int j)
{
int sum=0;
for(int k=0;k<7;k++)
if(str[i][k]!=str[j][k])
sum++;
return sum;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
init();
vector<Edge> d;
for(int i=0;i<n;i++)
scanf("%s",str[i]);
// for(int i=0;i<n;i++)
// printf("%s\n",str[i]);
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
d.push_back(Edge(i,j,sdis(i,j)));
sort(d.begin(),d.end(),cmp);
int sum=0,cnt=0;
// printf("%d\n",d.size());
for(int i=0;i<d.size();i++)
{
Edge e=d[i];
// printf("%d->%d:%d\n",e.s,e.e,e.dis);
if(join(e.s,e.e))
{
sum+=e.dis;
cnt++;
}
if(cnt==n-1) break;
}
printf("The highest possible quality is 1/%d.\n",sum);
}
return 0;
}

在Advanced Cargo Movement的历史中,不同类型的卡车通过特定的代码描述其特性。历史学家试图找到所谓的衍化计划,即如何从一种类型的卡车衍生出其他类型,以确定衍化过程的质量。这个问题被转化为寻找具有最高可能质量的衍化计划,其中质量定义为所有衍生对距离的倒数之和的倒数。本文介绍了一种算法解决方案,利用最小生成树的概念来解决这一问题。
384

被折叠的 条评论
为什么被折叠?



