poj1789

Truck History

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10998 Accepted: 4092

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.
 
1次AC就是爽撒。
题目中生词太多了,有点看不懂,不过还好,还是懂了
就是有n辆车,每辆车都有一个7位的编号,两个编号之间的d代表这两个编号之间不同字母的个数。
一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的d,
现在要找出一个“衍生”方案,使得总代价最小,也就是d之和最小。
怎么做呢
以任意两个车的编号作为节点,这条边的边权为两个编号间不同字母的个数,
这样图就建好了,至于用prim还是用kruskal,就无所谓了
我用的prim,矩阵来存图,好费空间啊
这题确实水了,连我都能1A
 1#include<stdio.h>
 2#include<string.h>
 3#include<math.h>
 4#define MAX 2100
 5char s[MAX][7];
 6int map[MAX][MAX];
 7int n,ans,cost[MAX];
 8short vis[MAX];
 9int cmp(char s1[],char s2[])
10{
11    int i,tot;
12    tot=0;
13    for (i=0; i<=6 ; i++ )
14        if (s1[i]!=s2[i]) tot++;
15    return tot;
16}

17void init()
18{
19    int i,j;
20    for (i=1; i<=n ; i++ )
21        scanf("%s",&s[i]);
22    memset(map,0,sizeof(map));
23    for (i=1; i<=n-1 ; i++ )
24        for (j=i+1; j<=n; j++)
25        {
26            map[i][j]=cmp(s[i],s[j]);
27            map[j][i]=map[i][j];
28        }

29}

30void prim()
31{
32    int i,j,mini,min;
33    memset(vis,0,sizeof(vis));
34    for (i=2; i<=n; i++) cost[i]=map[1][i];
35    vis[1]=1;
36    ans=0;
37    for (i=1; i<=n-1 ; i++ )
38    {
39        min=0x7fffffff;
40        for (j=1; j<=n ; j++ )
41            if ((!vis[j])&&(cost[j]<min))
42            {
43                min=cost[j];
44                mini=j;
45            }

46        ans=ans+min;
47        vis[mini]=1;
48        for (j=1; j<=n ; j++ )
49            if ((!vis[j])&&(map[mini][j]>0)&&(map[mini][j]<cost[j]))
50                cost[j]=map[mini][j];
51    }

52}

53int main()
54{
55    scanf("%d",&n);
56    while (n!=0)
57    {
58        init();
59        prim();
60        printf("The highest possible quality is 1/%d.\n",ans);
61        scanf("%d",&n);
62    }

63    return 0;
64}

65

posted on 2012-02-14 19:03 jh818012 阅读(277) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年7月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论