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. GTX 750等低配显卡如何玩转Deepfakes?

    这里说的Deepfakes软件还是DeepFaceLab,人工智能换脸,是使用深度学习方法来实现的.而深度学习程序对电脑配置要求是非常高的,尤其是跑模型这个环节.很多低配电脑,根本就跑步起来.比如像G ...

  2. LeetCode(164)Maximum Gap

    题目 Given an unsorted array, find the maximum difference between the successive elements in its sorte ...

  3. rs485多主

    因复位时I/O口都输出高电平.如果把I/O口直接与RS-485接口芯片的驱动器使能端DE端相连,会在CPU复位其间DE为高,从而使本节点处于发送状态.如果此时总线上其它节点在发送数据,则此次数据传输将 ...

  4. golang json 示例

    jsonStr, err := client.Get( deviceIdKey ).Result() if err == redis.Nil { deviceIds = []string{device ...

  5. <原创> 通过PEB获得进程路径 (附完整工程)

    完整工程:http://files.cnblogs.com/files/Gotogoo/%E8%BF%9B%E7%A8%8B%E7%AE%A1%E7%90%86%E5%99%A8%28x86%26%2 ...

  6. HDU 2852 KiKi's K-Number 主席树

    题意: 要求维护一个数据结构,支持下面三种操作: \(0 \, e\):插入一个值为\(e\)的元素 \(1 \, e\):删除一个值为\(e\)的元素 \(2 \, a \, k\):查询比\(a\ ...

  7. UVa 11552 DP Fewest Flops

    题解 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...

  8. python 提交form-data之坑

    #coding=utf-8 import requests from requests_toolbelt import MultipartEncoder #requests库上传 files = {& ...

  9. mysql 索引和查询优化

    对于任何DBMS,索引都是进行优化的最主要的因素.对于少量的数据,没有合适的索引影响不是很大,但是,当随着数据量的增加,性能会急剧下降.如果对多列进行索引(组合索引),列的顺序非常重要,MySQL仅能 ...

  10. 移动端click时间、touch事件、tap事件详解

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...