POJ 1789 Truck History(构造最小生成树)

本文介绍了一个基于Kruskal算法的解决方案,用于寻找一组卡车类型的最优衍生计划。通过计算不同类型卡车代码间的距离,并构造最小生成树来确定最佳衍生路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=1789

Truck History
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 27396 Accepted: 10651
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

CTU Open 2003

【中文题意】给你n个单词,每两个个单词之间的距离为这两个单词中字母不同的个数(即比较每一位,不能对字母排序后再比较),然后由一个字母衍生出其他n-1个字母的花费是多少。
【思路分析】我们根据题意可以把这个题转变一下思维,从某一个点开始,然后可以到达其他n-1个点,这样不是最小生成树么,然后我们可以建边,用Kruskal算法搞一下。
【AC代码】

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;

int pre[2005];

int fin(int x)
{
    if(x==pre[x])
    {
        return x;
    }
    else
    {
        return pre[x]=fin(pre[x]);
    }
}

void join(int x,int y)
{
    if(fin(x)!=fin(y))
    {
        pre[fin(x)]=pre[fin(y)];
    }
}

char a[2005][8];
struct node
{
    int u,v,cost;
}p[4000005];

bool cmp(node x,node y)
{
    return x.cost<y.cost;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",&a[i]);
            pre[i]=i;
        }
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                int s=0;
                for(int k=0;k<7;k++)
                {
                    if(a[i][k]!=a[j][k])
                    {
                        s++;
                    }
                }
                p[cnt].u=i;
                p[cnt].v=j;
                p[cnt++].cost=s;
                //printf("%d\n",p[cnt-1].cost);
            }
        }
        sort(p,p+cnt,cmp);
        int s=0,sum=0;
        for(int i=0;i<cnt;i++)
        {
            if(fin(p[i].u)!=fin(p[i].v))
            {
                join(p[i].u,p[i].v);
                s++;
                sum+=p[i].cost;
            }
            if(s==n-1)break;
        }
        printf("The highest possible quality is 1/%d.\n",sum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值