Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20884   Accepted: 8075

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.
 #include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x0fffffff;
char str[][];
int map[][];
int vis[];
int d[];
int n;
int Prim(){
for(int i=;i<n;i++){
d[i]=INF;
vis[i]=;
}
d[]=;
int sum=;
for(int i=;i<n;i++){
int mi=INF;
int p;
for(int j=;j<n;j++){
if(!vis[j]&&mi>d[j]){
mi=d[j];
p=j;
}
}
sum+=mi;
vis[p]=;
for(int j=;j<n;j++){
if(!vis[j]){
d[j]= min(d[j],map[p][j]);
}
}
}
return sum;
}
int main() {
cin>>n;
while(n){
for(int i=;i<n;i++){
for(int j=;j<;j++){
cin>>str[i][j];
}
}
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
int sum=;
for(int k=;k<;k++){
if(str[i][k]!=str[j][k]){
sum++;
}
}
map[i][j]=map[j][i]=sum;
}
}
int sum=Prim();
cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
cin>>n;
}
return ;
}

Truck History - poj 1789 (Prim 算法)的更多相关文章

  1. F - Truck History - poj 1789

    有一个汽车公司有很多年的汽车制造历史,所以他们会有很多的车型,现在有一些历史学者来研究他们的历史,发现他们的汽车编号很有意思都是有7个小写字母组成的,而且这些小写字母具有一些特别的意义,比如说一个汽车 ...

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

    链接: http://poj.org/problem?id=1789 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2213 ...

  3. Truck History POJ - 1789

    题目链接:https://vjudge.net/problem/POJ-1789 思路: 题目意思就是说,给定一些长度为7的字符串,可以把字符串抽象为一个点, 每个点之间的距离就是他们本身字符串与其他 ...

  4. Truck History POJ - 1789 板子题

    #include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> u ...

  5. POJ1789 Truck History 【最小生成树Prim】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18981   Accepted: 7321 De ...

  6. POJ-1789 Truck History---最小生成树Prim算法

    题目链接: https://vjudge.net/problem/POJ-1789 题目大意: 用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一 ...

  7. Agri-Net - poj 1258 (Prim 算法)

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44373   Accepted: 18127 Description F ...

  8. Highways - poj 2485 (Prim 算法)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24383   Accepted: 11243 Description T ...

  9. POJ 1789 Prim

    给定N个字符串,某个字符串转为另一个字符串的花费为他们每一位不相同的字符数. 求最小花费Q. Input 多组输入,以0结束. 保证N不超过2000. Output 每组输出"The hig ...

随机推荐

  1. luogu P1195 口袋的天空

    题目背景 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空. 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖. 题目描述 给你云朵的个数N,再给你M个关系,表示哪些云朵可以连在 ...

  2. String&&StringBuilder&&StringBuffer

    在java中提供三个操作字符串的类:String,StringBuilder,StringBuffer (1)什么是字符串:多个字符的集合 (2)String 是内容不可变的字符串.(底层使用了一个不 ...

  3. PHP的一些陷阱

    1,空Array为False $a = array() if($a == false) { echo "This will be printed ! ! !" ; } 持续更新中

  4. linux之rpm指令

    rmp原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM套件管理方式的出现,让Linux易于 ...

  5. NAND_FLASH_内存详解与读写寻址方式

    一.内存详解 NAND闪存阵列分为一系列128kB的区块(block),这些区块是 NAND器件中最小的可擦除实体.擦除一个区块就是把所有的位(bit)设置为"1"(而所有字节(b ...

  6. iOS8使用TestFlight进行内部测试功能尝鲜

    iOS8发布了有一段时间了,我们的策划很新潮的速度给升级到iOS8了.于是XCode5不支持了,只好从MacOS 10.8升级到10.9,再升级到10.9.5,再下载XCode6安装…… 然后前两天上 ...

  7. C# this.Invoke()的作用与用法

    Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke();   在多线程编程中,我们经常要在工作线程 ...

  8. 走进C++程序世界-----operator new delete 重载

     在C++ 的世界里,new 和delete 是keyword.而在C的世界里相相应的malloc和free是函数,关键C++的new和delete分析,详见前面的章节.这里就不在过多的介绍了.链接. ...

  9. hive bucket

    转载:https://www.cnblogs.com/end/archive/2013/01/09/2852413.html hive中table可以拆分成partition,table和partit ...

  10. mac os x+paralles使用source insight

    将Mac OS X下的目录共享到Paralles后,source insight创建工程.但是当再次打开时却打开失败.提示:there was an error opening project 网上对 ...