Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19122   Accepted: 7366

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.

Source

 
#include <iostream>
#include <cstring> using namespace std;
char str[2001][9];
int d[2001][2001];
int mind[2001];//mind restore the min distance of the point between the tree when the point hasn't been add into
//rather than the point in the tree to all the point not in the tree
bool vis[2001];
int n;
int ans;
int index;
void setd(){
for(int i=0;i<n;i++){
d[i][i]=0;
for(int j=i+1;j<n;j++){
d[i][j]=d[j][i]=0;
for(int k=0;k<7;k++){
if(str[i][k]!=str[j][k]){
d[i][j]++;
}
}
d[j][i]=d[i][j];
}
}
}
void prim(){
memset(mind,0x3f,sizeof(mind));
memset(vis,0,sizeof(vis));
index=1;
vis[0]=true;
ans=0;
int s=0;
while(index<n){
int minnum=0x3f3f;
int mini=0;
for(int i=0;i<n;i++){
if(!vis[i]&&d[s][i]<mind[i]){
mind[i]=d[s][i];//use i to refresh all the point
}
if(!vis[i]&&mind[i]<minnum){
minnum=mind[i];
mini=i;
}
}
ans+=minnum;
vis[mini]=true;
s=mini;
index++;
}
}
int main(){
ios::sync_with_stdio(false);
while(cin>>n&&n){
for(int i=0;i<n;i++){
cin>>str[i];
}
setd();
prim();
cout<<"The highest possible quality is 1/"<<ans<<".\n";
} return 0;
}

  

poj 1789 Truck History 最小生成树 prim 难度:0的更多相关文章

  1. POJ 1789 -- Truck History(Prim)

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

  2. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

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

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

  4. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  5. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  6. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

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

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

  8. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

  9. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

随机推荐

  1. Python开发【模块】:time、datatime

    时间模块 时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11,   ...

  2. SQL基础--查询之一--单表查询

    SQL基础--查询之一--单表查询

  3. Nginx rewrite 中break与last指令的区别

    location /break/ { rewrite ^/break/(.*) /test/$1 break; return 402; } location /last/ { rewrite ^/la ...

  4. python练习汇总

    1.99乘法表 """ 题目:输出 9*9 乘法口诀表. """ for i in range(1, 10): print () for j ...

  5. java, android的aes等加密库

    https://github.com/scottyab/AESCrypt-Android https://github.com/PDDStudio/EncryptedPreferences       ...

  6. boost pool 和 object_pool

    内存池(Memory Pool)是一种内存分配方式.        通常我们习惯直接使用new.malloc等API申请分配内存,这样做的缺点在于:由于所申请内存块的大小不定,当频繁使用时会造成大量的 ...

  7. Xcel 测试版使用手册

    基于无任何文笔可言,所以直接上使用方法吧. 1.引用dll,如何引用dll请谷歌. 2.使用 //实例化对象 LT.XMLExcel.XlsxOption xOption = new LT.XMLEx ...

  8. vs2010中如何编写C语言程序

    File->New->Project 在打开的New Project对话框中最左侧一栏中选择Visual C++下面的CLR,之后在其右侧的区域中选择CLR Empty Applicati ...

  9. DHCP服务器配置实践

    实验背景:在LINUX系统上为一园区网络配置DHCP服务器,给网络内各主机自动分配IP地址,地址池范围为:192.168.X.100~192.168.X.200,配置作用域选项,其中网关为:192.1 ...

  10. SQL :模糊查询,转义字符

    1. 查询table表name列包含 '_BCE' 的记录 select * from table where name like '_BCE%' ABCEDF _BCEFG _BCEDF 3 row ...