POJ 1789 Truck History【最小生成树prim/kruskal+稠密图】

题目链接

Truck History

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 39547 Accepted: 15113

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.

思路

看懂题的话,就明白是一道裸的最小生成树
题目大意是:给你n辆卡车,每辆由七个字符组成,把每个车当作一个节点,然后加边,生成一个完全图
边的权重是不同卡车的“距离”,题意是同一位置不同字符的个数
如baaaaab和aaaaaaa,第1位和第7位不同,所以距离为2
最后求最小生成树
我尝试了两种方法,即prim和kruskal,令我不解的是,两种耗时差不多,不应该啊,这是个稠密图啊!也许是我写法的问题吧!
其实还写了prim的优先队列优化,不过好像错了点,就不放了。
Tip1:这里用char二维数组耗时比string一维数组少了好多,以后尽量不用string。
Tip2:输入输出多的时候尽量别用cin/cout,慢,我这里没改。
emmm因为是板子题,就不加注释了!!!

prim算法
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <queue>
#include <memory.h>
#define inf 0x3f3f3f3f
using namespace std;

int n;
int dis[2020][2020];
bool book[2020];
int lowd[2020];
char truck[2020][10];

int prim(){
    int sum=0;
    memset(book,false,sizeof(book));
    fill(lowd,lowd+2020,inf);
    lowd[1]=0;
    for(int i=1;i<=n;i++){
        int u=-1;
        int mn=inf;
        for(int j=1;j<=n;j++)
            if(!book[j]&&lowd[j]<mn){
                u=j;
                mn=lowd[j];
            }
        book[u]=true;
        sum+=lowd[u];
        for(int k=1;k<=n;k++)
            if(!book[k]&&lowd[k]>dis[u][k])
                lowd[k]=dis[u][k];
    }
    return sum;
}

int main()
{
    while(cin>>n){
        if(n==0)
            break;
        for(int i=1;i<=n;i++){
            cin>>truck[i];
            for(int j=i-1;j>=1;j--){
                    int d=0;
                    for(int k=0;k<=6;k++)
                        if(truck[i][k]!=truck[j][k])
                            d++;
                dis[i][j]=dis[j][i]=d;
            }
        }
        cout<<"The highest possible quality is 1/"<<prim()<<"."<<endl;
    }
    return 0;
}
kruskal算法
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <memory.h>
using namespace std;

struct edge{
    int from;
    int to;
    int value;
}a[2500001];

bool cmp(edge a,edge b){
    return a.value<b.value;
}

int f[2100];
int n,m;


int getf(int v){
    if(f[v]==v){
        return v;
    }
    else{
        f[v]=getf(f[v]);
        return f[v];
    }
}

void mrge(int v,int u){
    int t1=getf(v);
    int t2=getf(u);
    if(t1!=t2){
        f[t2]=t1;
    }
    return;
}

int kruskal(){
    int sum=0;
    int cnt=0;
    for(int i=1;i<=m;i++){
        int t1=getf(a[i].from);
        int t2=getf(a[i].to);
        if(t1!=t2){
            sum+=a[i].value;
            cnt++;
            mrge(t1,t2);
        }
        if(cnt==n-1)
            return sum;
    }
}

int main()
{
    while(cin>>n){
        if(n==0)
            break;
        char truck[2100][10];
        for(int i=1;i<=n;i++){
            cin>>truck[i];
        }
        m=0;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                int d=0;
                for(int k=0;k<=6;k++)
                    if(truck[i][k]!=truck[j][k])
                        d++;
                m++;
                a[m].from=i;
                a[m].to=j;
                a[m].value=d;
            }
        }
        for(int i=1;i<=n;i++)
            f[i]=i;
        sort(a+1,a+m+1,cmp);
        cout<<"The highest possible quality is 1/"<<kruskal()<<"."<<endl;
        }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值