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.

Hint

 

需要用普里母算法这种算法让我很是纠结,努力了一天才勉强搞定,很是伤自尊啊

下面是我同学的代码

 #include<iostream>
#include<cstdio>
using namespace std;
int grah[][];
int sum=;
void pim(int n)//用Kruskal会超时
{
int i,j,pos;
int min,v[]= {},d[];
for(i=; i<=n; i++)//
d[i]=grah[][i];
v[]=;
for(i=; i<=n; i++)
{
min=;
for(j=; j<=n; j++)
{
if(!v[j]&&min>d[j])
{
min=d[j];
pos=j;
}
}
sum+=min;
v[pos]=;
for(j=; j<=n; j++)
{
if(!v[j])
{
if(d[j]>grah[pos][j])
d[j]=grah[pos][j];
}
}
}
}
int main()
{
int n,i,j,k,cou;
char s[][];
while(cin>>n&&n)
{
getchar();
for(i=; i<=n; i++)
cin>>s[i];
for(i=; i<=n; i++)
{
for(j=i+; j<=n; j++)
{
cou=;
for(k=; k<; k++)
{
if(s[i][k]!=s[j][k])//如果有一个字母不同就+1
cou++;
}
grah[i][j]=grah[j][i]=cou;//标出权重
}
grah[i][i]=;//自己到自己的距离为0;
}
pim(n);
printf("The highest possible quality is 1/%d.\n",sum);
sum=;
}
return ;
}

Truck History(poj 1789)的更多相关文章

  1. Truck History(卡车历史)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26547   Accepted: 10300 Description Adv ...

  2. Truck History(kruskal+prime)

    Truck History Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

  3. poj 1789 Truck History(kruskal算法)

    主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...

  4. POJ 1789 Truck History(Prim+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  5. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

  6. POJ 1789:Truck History(prim&amp;&amp;最小生成树)

    id=1789">Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17610   ...

  7. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  8. Truck History(最小生成树)

    poj——Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27703   Accepted: 10 ...

  9. Truck History(prime)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31871   Accepted: 12427 D ...

随机推荐

  1. Codeforces Beta Round #51 D. Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. Android传感器概述(六)

    监视传感器事件 要监视原始的传感器数据,你须要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged().Andr ...

  3. I/O体系结构和设备驱动程序

    http://blog.csdn.net/kafeiflynn/article/category/789844

  4. linux调度器系列

    http://blog.csdn.net/wudongxu/article/category/791519

  5. Fix java version mismatch in windows---stackoverflow

    Question: I have the 64bit version of the jdk installed on windows 7. I installed the 32 bit version ...

  6. mysql默认用户名和密码

    默认用户名:root 密码:gree..

  7. 基于Html5的兼容所有主流浏览器的在线视频播放器videoJs

    在一个新的项目上需要实现在线视频播放,原本打算借助优酷的视频存储和播放,但是发现这个需要用户注册优酷账户,严重影响用户体验,于是这个方案被毙掉了.于是开始了自己开发一个在线播放器的想法,当然尽量使用已 ...

  8. oracle中drop、delete和truncate的区别

    oracle中drop.delete和truncate的区别 oracle中可以使用drop.delete和truncate三个命令来删除数据库中的表,网上有许多文章和教程专门讲解了它们之间的异同,我 ...

  9. HibernateTool的安装和使用(Eclipse中)

    http://blog.sina.com.cn/s/blog_919273e20101g1t7.html

  10. linux常用编辑器

    管理员在进行系统操作的时候,不可避免地会对文本进行修改,如进行各种服务程序配置文件的改动,使程序对用户提供不同的服务效果.在本章我们向大家介绍Linux上常见的编辑器ed.vi.emacs,同时以vi ...