[POJ](1789)Truck History ---最小生成树(图)

来源:互联网 发布:大数据支撑平台 编辑:程序博客网 时间:2024/05/17 07:20
Truck History
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 29148 Accepted: 11392

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

4aaaaaaabaaaaaaabaaaaaaabaaaa0

Sample Output

The highest possible quality is 1/3.


解题新知:

       ①感慨:这道题最小生成树的题目很好,题意比较难理解。我看了一位前辈的题解恍然大悟,这道题没我想的那么无脑,还是有一些巧妙的地方的。

       ②题意:以每i行的字符串(车牌)为基准,求第i行与其他行的差值,然后就惊奇的发现是一个加权无向图

        举例:  

                     abaaaba                       以第一行为基准与各行的差值为 0 2 6 1

                     baaaaba                       以第二行为基准与各行的差值为 2 0 4 3

                     baabbab                       以第三行为基准与各行的差值为 6 4 0 5

                     abaaaaa                       以第四行为基准与各行的差值为 1 3 5 0

         呀!我们惊奇的发现这不是构成了一个邻接矩阵(加权无向图)吗!

        ③问:每个车牌的之间都有权值,求得一个所有车牌间的总权值Q,使得1/Q为高质量,说明Q最小,即最小生成树!


参考资料:http://www.cnblogs.com/Tree-dream/p/5565072.html


AC代码:

#include<iostream>#include<cstdio>#include<cstring>#define INF 0x3f3f3f3fusing namespace std;int mmap[2005][2005];bool vis[2005];int dist[2005];int n;char str[2005][10];int sum;void creatMap() //将给的字符串信息转换成图{    int num;    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            num=0;            for(int k=0;k<7;k++)            {                if(str[i][k]!=str[j][k])                    num++;            }            mmap[i][j]=num;        }    }}void prim(){    for(int i=1;i<=n;i++)        dist[i]=mmap[1][i];    for(int i=1;i<=n;i++)        mmap[i][i]=0;    int k,mmin;    for(int i=1;i<=n;i++)    {        mmin=INF;        for(int j=1;j<=n;j++)        {            if(!vis[j] && dist[j]<mmin)            {                mmin=dist[j];                k=j;            }        }        sum+=mmin;        vis[k]=true;        for(int j=1;j<=n;j++)        {            if(!vis[j] && mmap[k][j]<dist[j])                dist[j]=mmap[k][j];        }    }}int main(){    while(scanf("%d",&n)!=EOF&& n)    {        sum=0;        memset(vis,false,sizeof(vis));        memset(dist,0,sizeof(dist));        memset(mmap,0x3f,sizeof(mmap));        for(int i=1;i<=n;i++)            scanf("%s",str[i]);        creatMap();        /*for(int i=1;i<=n;i++)        {            for(int j=1;j<n;j++)                cout<<mmap[i][j]<<" ";            cout<<mmap[i][n]<<endl;        }*/        prim();        printf("The highest possible quality is 1/%d.\n",sum);    }    return 0;}


原创粉丝点击