码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1789 Truck History

时间:2014-07-10 23:51:01      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:poj   acm   

Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17275   Accepted: 6623

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.


题意:每两个字符串对应位置上字母不同的个数作为这两个字符串的距离。

经典最小生成树,这里提供两种做法,kruskal算法和prim算法。


AC代码如下:


kruskal算法!!!!!!!!!!


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

struct H{
    int l,r,d;
}w[5000005];

char map[2005][10];
int f[2005];

int find (int a)
{
    return f[a]==a? a : f[a]=find(f[a]);
}

bool cmp(H a, H b)
{
    return a.d<b.d;
}

int main()
{
    int n;
    int i,j,l;
    int ans;
    while(cin>>n,n)
    {
        int cur=0;
        for(i=1;i<=n;i++)
        {
            f[i]=i;
            cin>>map[i];
            for(j=1;j<=i;j++)
            {
                int t=0;
                for(l=0;l<7;l++)
                    if(map[i][l]!=map[j][l])
                        t++;
                w[cur].l=i;
                w[cur].r=j;
                w[cur].d=t;
                //cout<<w[cur].l<<" "<<w[cur].r<<" "<<w[cur].d<<endl;
                cur++;
            }
        }
        int ans=0;
        sort(w,w+cur,cmp);
        for(i=0;i<cur;i++)
        {
            int xx,yy;
            xx=find(w[i].l);
            yy=find(w[i].r);
            if(xx!=yy)
            {
                ans+=w[i].d;
                f[xx]=yy;
                //cout<<ans<<endl;
            }
        }
        printf("The highest possible quality is 1/%d.\n",ans);
    }
    return 0;
}



prim算法!!!!!!!!!!!

#include<iostream>
#include<algorithm>
#include<cstdio>
#define inf 100000000
using namespace std;

char map[2005][10];
int f[2005];
int w[2005][2005];
int lc[2005],cc[2005];
int minn,minid;

int main()
{
    int n;
    int i,j,l;
    int ans;
    while(cin>>n,n)
    {
        int cur=0;
        for(i=1;i<=n;i++)
        {
            f[i]=i;
            cin>>map[i];
            for(j=1;j<=i;j++)
            {
                int t=0;
                for(l=0;l<7;l++)
                    if(map[i][l]!=map[j][l])
                        t++;
                        w[i][j]=t;
                        w[j][i]=t;
            }
        }
        int ans=0;
        for(i=1;i<=n;i++)
        {
            lc[i]=w[1][i];
            cc[i]=1;
            //cout<<lc[i]<<" "<<cc[i]<<endl;
        }
        for(i=1;i<n;i++)
        {
            for(j=1,minn=inf;j<=n;j++)
            {

                if(lc[j]!=0&&lc[j]<minn)
                {minn=lc[j];minid=j;}
            }
            ans+=minn;
            //cout<<ans<<" "<<minid<<endl;
            lc[minid]=0;
            for(j=1;j<=n;j++)
            {
                if(lc[j]!=0&&w[minid][j]<lc[j])
                {
                    lc[j]=w[minid][j];
                    cc[j]=minid;
                }
            }
        }
        printf("The highest possible quality is 1/%d.\n",ans);
    }
    return 0;
}




POJ 1789 Truck History,布布扣,bubuko.com

POJ 1789 Truck History

标签:poj   acm   

原文地址:http://blog.csdn.net/hanhai768/article/details/37599179

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!