• Truck History--poj1789


    Truck History
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 21534   Accepted: 8379

    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.



    这题,我是没看懂,后来看的别人的翻译才自己做的!

     > 题意大概是这样的:用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。

    > 例如有如下4个编号:

    > aaaaaaa

    > baaaaaa

    > abaaaaa

    > aabaaaa

    > 显然的,第二,第三和第四编号分别从第一编号衍生出来的代价最小,因为第二,第三和第四编号分别与第一编号只有一个字母是不同的,相应的distance都是1,加起来是3。也就是最小代价为3。


    显然,最小生成树!



     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 int per[2010],n;
     7 struct node
     8 {
     9     int b,e,w;
    10 }s[2000500];//我开的挺大的,怕不够
    11 bool cmp(node x,node y)
    12 {
    13     return x.w<y.w;
    14 }
    15 void init()
    16 {
    17     for(int i=0;i<=n;i++)
    18     per[i]=i;
    19 }
    20 int find(int x)
    21 {
    22     int i=x,j;
    23     while(x!=per[x])
    24     x=per[x];
    25     while(i!=x)//我压缩了路径踩过的,超时了好多次
    26     {
    27         j=per[i];
    28         per[i]=x;
    29         i=j;
    30     }
    31     return x;
    32 }
    33 bool join(int x,int y)
    34 {
    35     int fx=find(x);
    36     int fy=find(y);
    37     if(fx!=fy)
    38     {
    39         per[fx]=fy;
    40         return true;
    41     }
    42     return false;
    43 }
    44 int main()
    45 {
    46     char map[2010][10];
    47     while(scanf("%d",&n),n)
    48     {
    49         getchar();
    50         init();
    51         int i,j;
    52         for(int i=0;i<n;i++)
    53         scanf("%s",map[i]);
    54         int k=0,sum=0,t;
    55         for(i=0;i<n;i++)
    56         {
    57             for(j=i+1;j<n;j++)
    58             {
    59                 int cot=0;
    60                 for(t=0;t<7;t++)
    61                 {
    62                     if(map[i][t]!=map[j][t])
    63                     cot++;    
    64                 }
    65                 s[k].b=i;
    66                 s[k].e=j;
    67                 s[k].w=cot;
    68                 k++;
    69             }
    70         }
    71         sort(s,s+k,cmp);
    72         for(i=0;i<k;i++)
    73         {
    74             if(join(s[i].b,s[i].e))
    75             sum+=s[i].w;
    76         }
    77         printf("The highest possible quality is 1/%d.
    ",sum);
    78     }
    79     return 0;
    80 }
    欢迎留言。
  • 相关阅读:
    arthas常用命令记录
    idea 的 http-client
    springboot 接口层参数校验 自定义参数校验
    Spring AOP 实现——使用annotation、pointcut、aspect
    Redis 热点名词
    SpringCloud注册发现配置
    【设计模式】-行为型-11-解释器模式
    【设计模式】-行为型-10-备忘录模式
    【设计模式】-创建型-9-访问者模式
    Kubernetes运行原理
  • 原文地址:https://www.cnblogs.com/Eric-keke/p/4722103.html
Copyright © 2020-2023  润新知