poj——Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27703   Accepted: 10769

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.

Source

 
 
代码:
#include<cstdlib>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N  2001
using namespace std;
struct Edge
{
    int x,y,z;
}edge[N*N];
int n,m,fa[N],ans,sum,tot;
];
int cmp(Edge a,Edge b)
{
    return a.z<b.z;
}
int found(int x)
{
    return fa[x]==x?x:fa[x]=found(fa[x]);
}
int main()
{
    while(scanf("%d",&n),n)
    {
        memset(a,,sizeof(a));
        ans=;
        ;i<=n;i++)
          cin>>a[i];
        tot=;
        ;i<n;i++)
         ;j<=n;j++)
         {
             sum=;
             ;k<;k++)
                 if(a[i][k]!=a[j][k])
               sum++;
            edge[++tot].x=i;
            edge[tot].y=j;
            edge[tot].z=sum;
          }
        sort(edge+,edge++tot,cmp);
        ;i<=n;i++)
         fa[i]=i;
        sum=;
        ;i<=tot;i++)
        {
            int x=edge[i].x,y=edge[i].y;
            int fx=found(x),fy=found(y);
            if(fx!=fy)
            {
                fa[fy]=fx;
                sum++;
                ans+=edge[i].z;
            }
            ) break;
        }
        printf("The highest possible quality is 1/%d.\n",ans);
    }
    ;
}
 

Truck History(最小生成树)的更多相关文章

  1. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

  2. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

  3. poj1789 Truck History最小生成树

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  4. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  5. POJ 1789:Truck History(prim&amp;&amp;最小生成树)

    id=1789">Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17610   ...

  6. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

  8. POJ1789 Truck History 【最小生成树Prim】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18981   Accepted: 7321 De ...

  9. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

随机推荐

  1. 如何在微信中发送"相册"文件时有选择性地显示视频文件

    相信很多微信用户在使用微信给朋友,同事发送相册中的文件时,微信会显示你手机中的视频文件,这样很不方便. 如果要完全不显示视频文件: 随便在手机中建立一个文件夹,名字叫 ".nomedia&q ...

  2. bin、hex、elf、axf文件的区别

    1.bin Bin文件是最纯粹的二进制机器代码, 或者说是"顺序格式".按照assembly code顺序翻译成binary machine code,内部没有地址标记.Bin是直 ...

  3. Hadoop4.2HDFS测试报告之三

    第一组:文件存储写过程记录 NameNode:1 DataNode:1 本地存储 scp localpath romotepath 500 2 1 23.67 NameNode:1 DataNode: ...

  4. BZOJ 5064: B-number

    数位DP #include<cstdio> #include<cstring> using namespace std; int A[16]; long long F[16][ ...

  5. python基础补漏-08-异常处理

    try: #正常代码逻辑 ins = raw_input("this is a tast:") print ins+1 except Exception,e: print e -- ...

  6. Learning Deconvolution Network for Semantic Segme小结

    题目:Learning Deconvolution Network for Semantic Segmentation 作者:Hyeonwoo Noh, Seunghoon Hong, Bohyung ...

  7. Leetcode 451.根据字符出现频率排序

    根据字符出现频率排序 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次 ...

  8. DS-博客作业06--图

    DS-博客作业06--图 1.本周学习总结(0--2分) 1.思维导图 2.谈谈你对图结构的认识及学习体会. 本章的图,因为和上一章的树上的比较紧凑,所以在考前一个星期也有看书背代码,也有理解代码和思 ...

  9. 【bzoj3698】XWW的难题 有上下界最大流

    题目描述 XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核.XWW给你出了这么一个难题:XWW给你一个N*N的正实数矩阵A ...

  10. 关闭chrome浏览器的input香蕉黄背景

    chrome浏览器input的自动完成,点击之后自动输入,input的背景会变成香蕉黄,用如下方法修复: /* Change the white to any color ;) 就是给input设置内 ...