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. Burnside引理与polay定理

    #Burnside引理与polay定理 引入概念 1.置换 简单来说就是最元素进行重排列 是所有元素的异议映射,即\([1,n]\)映射到\([1,n]\) \[ \begin{pmatrix} 1& ...

  2. spring-----mvc的反射调用

    这几天在看spring mvc 源码,一直很好奇它究竟在哪进行的反射调用,通过源码,仔细阅读,最后发现了调用位置在类 InvocableHandlerMethod 的doInvoke 方法 /** * ...

  3. 八. 输入输出(IO)操作5.面向字节流的应用

    文件输入输出流 文件输入输出流 FileInputStream 和 FileOutputStream 负责完成对本地磁盘文件的顺序输入输出操作. [例 10-5]通过程序创建一个文件,从键盘输入字符, ...

  4. 数值计算方法 | C语言实现几个数值计算方法(实验报告版)

    目录 写在前面 实验一 牛顿插值方法的实现 实验二 龙贝格求积算法的实现 实验三 高斯列主元消去法的实现 实验四 最小二乘方法的实现 写在前面 使用教材:<数值计算方法>黄云清等编著 科学 ...

  5. [置顶] kubernetes--应用程序健康检查

    K8S的应用程序健康检查分为livenessProbe和readinessProbe,两者相似,但两者存在着一些区别. livenessProbe在服务运行过程中检查应用程序是否运行正常,不正常将杀掉 ...

  6. 设计模式之建造者模式(php实现)

    github地址:https://github.com/ZQCard/design_pattern/** * 建造者模式 * 将一个复杂对象的建造与调用者分离.调用者只需要给出指定对象的类型和内容,建 ...

  7. zoj 3430 Detect the Virus(AC自己主动机)

    题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...

  8. ambari修改admin密码

    https://community.hortonworks.com/questions/449/how-to-reset-ambari-admin-password.html 1) Postgres ...

  9. 读取Properties文件简易代码

    public class Utils { private static Properties props = new Properties(); static { ClassLoader classL ...

  10. golang sync.Cond 类

    众所周知,go语言在多线程方面的支持是十分完备的.在go语言sync包中提供了一个Cond类,这个类用于goroutine之间进行协作. 这个类并不复杂,只有三个函数,Broadcast() , Si ...