Truck History 【POJ--1789】【普里姆&&克鲁斯卡尔】【邻接矩阵】

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.
题意:多组输入,每组输入n(n=0时程序结束)表示有n辆卡车,除了第一辆卡车其他每辆卡车都是由其他卡车改造而来的,接下来有n行字符串代表每辆卡车的编号,任意两辆车之间字符串相同位置字母不同的个数即两辆车之间的“距离”,求n辆卡车的最小总“距离”。
心得:求最小生成树总共有两种算法,一种是Prim,一种是Kruskal。前者适合稠密图,其枚举的是顶点;后者适合稀疏图,其枚举的是边。

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

Prim

<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
int mmap[2002][2002],n;
int Get(char *a,char *b)                    //求任意两辆车之间的“距离”
{
    int cnt=0;
    int len=strlen(a);
    for(int i=0; i<len; i++)
        if(a[i]!=b[i])
            cnt++;
    return cnt;
}
char st[2002][10];
bool vis[2002];
int dis[2002];
int Prim()                          //类似于dijkstra算法
{
    int sum=0,mm,k;
    memset(vis,0,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    dis[0]=0;
    for(int i=0; i<n; i++)
    {
        mm=INF;
        k=-1;
        for(int j=0; j<n; j++)                   //先找权最小的边加入到树中
        {
            if(!vis[j]&&dis[j]<mm)
            {
                mm=dis[j];
                k=j;
            }
        }
        vis[k]=true;
        sum+=dis[k];
        for(int j=0; j<n; j++)
        {
            if(!vis[j]&&dis[j]>mmap[k][j])
            {
                dis[j]=mmap[k][j];
            }
        }

    }
    return sum;
}
int main()
{
   // freopen("lalala.text","r",stdin);
    while(~scanf("%d",&n))
    {
        if(n==0)
            break;
        int i,j;
        for(i=0; i<n; i++)
        {
            scanf("%s",st[i]);
            for(j=0; j<i; j++)         
            {
                mmap[j][i]=mmap[i][j]=Get(st[i],st[j]);    //对于Prim算法,建图建的是无向图
            }
            mmap[i][j]=0;
        }
        printf("The highest possible quality is 1/%d.\n",Prim());
    }
    return 0;
}</span>


Kruskal

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef struct Edge
{
    int from,w,to;
    //Edge() {}
   // Edge(int a,int b,int c):from(a),w(b),to(c) {}
    bool operator < (const Edge aa)const   //重载运算符,当排序时,自动按升序排序
    {
        return w<aa.w;
    }
} Edge;
Edge eg[2000000];
char st[2002][10];
int pr[2002],n;
int Get(char *a,char *b)
{
    int cnt=0;
    for(int i=0; i<7; i++)
        if(a[i]!=b[i])
            cnt++;
    return cnt;
}
int Find(int x)
{
    if(pr[x]!=x)
        pr[x]=Find(pr[x]);
    return pr[x];
}
int main()
{
    // freopen("lalala.text","r",stdin);
    int cnt,sum,x,y,i,j;
    while(~scanf("%d",&n))
    {
        if(n==0)
            break;
        int tp=0;
        for(i=0; i<n; i++)
        {
            scanf("%s",st[i]);
            for(j=0; j<i; j++)                 //建立单向边
            {
                eg[tp].from=j;
                eg[tp].to=i;
                eg[tp++].w=Get(st[i],st[j]);
            }
        }
        sort(eg,eg+tp);
        for(i=0; i<n; i++)                    //初始化
            pr[i]=i;
        cnt=sum=0;
        for(i=0; i<tp; i++)                   //枚举边
        {
            x=Find(eg[i].from);
            y=Find(eg[i].to);
            if(x!=y)                               //如果该边的两个顶点不在一个集合内,则将该边加入到树中
            {
                pr[x]=y;                          //将该边的两个顶点归并到一个集合内
                cnt++;                            //累计边数
                sum+=eg[i].w;               //累计总权数
            }
            if(cnt==n-1)                       //如果边数够了直接跳出
                break;
        }
        printf("The highest possible quality is 1/%d.\n",sum);
    }
    return 0;
}<strong>
</strong>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值