Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19772   Accepted: 7633

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<stdio.h>
#include<string.h>
const int inf = 0x3f3f3f3f ;
int a[][] ;
char st[][] ;
int d[] ;
bool p[] ;
int n ; void prim ()
{
for (int i = ; i <= n ; i++) {
d[i] = a[][i] ;
p[i] = ;
}
d[] = ;
int ans = ;
for (int i = ; i < n ; i++) {
int minc = inf , k ;
for (int j = ; j <= n ; j++) {
if (d[j] && d[j] < minc) {
minc = d[j] ;
k = j ;
// printf ("d[%d]= %d\n" , j , d[j]) ;
}
}
d[k] = ;
for (int j = ; j <= n ; j++) {
if (d[j] && d[j] > a[k][j]) {
d[j] = a[k][j] ;
p[j] = k ;
}
}
ans += minc ;
}
printf ("The highest possible quality is 1/%d.\n" , ans) ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
while (~ scanf ("%d" , &n)) {
if (n == )
break ;
getchar () ;
for (int i = ; i <= n ; i++)
for (int j = ; j <= n ;j++)
a[i][j] = inf ;
for (int i = ; i <= n ; i++)
gets (st[i]) ;
for (int i = ; i <= n ; i++) {
int cnt = ;
for (int j = i + ; j <= n ; j++) {
for (int k = ; k < ; k++) {
if (st[i][k] != st[j][k]) {
cnt ++ ;
}
}
a[i][j] = a[j][i] = cnt ;
cnt = ;
}
}
prim () ;
}
return ;
}

Truck History(prim & mst)的更多相关文章

  1. POJ1789 Truck History(prim)

    题目链接. 分析: 最大的敌人果然不是别人,就是她(英语). 每种代表车型的串,他们的distance就是串中不同字符的个数,要求算出所有串的distance's 最小 sum. AC代码如下: #i ...

  2. Truck History(prim)

    http://poj.org/problem?id=1789 读不懂题再简单也不会做,英语是硬伤到哪都是真理,sad++. 此题就是一个最小生成树,两点之间的权值是毎两串之间的不同字母数. #incl ...

  3. POJ 1789 -- Truck History(Prim)

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

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

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

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

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

  6. poj 1789 Truck History 最小生成树 prim 难度:0

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

  7. Kuskal/Prim POJ 1789 Truck History

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

  8. poj1789 Truck History

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  9. poj 1789 Truck History 最小生成树

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

随机推荐

  1. sql server 2008 登录 4064 错误解决办法

    出现这个错误是账户无法打开默认数据库导致的 修改一下该账户的默认打开数据库即可. 如果很不幸,你的sa帐号和windows身份验证默认都打开同一个数据库,那么无论换哪种方式登录都是一样没用的 如果你有 ...

  2. Linux初探

    终于心血来潮装了Ubuntu,向着正式程序员迈出了重要一步.不得不说Linux真是一个磨人的小妖精,这篇随笔记录了一些我遇到的问题和解决方法. 1.Ubuntu安装 不知道听谁说的Linux难装,一不 ...

  3. 内置模块加载器(commonjs规范)的使用

    index9.html <html><head> <title>模块加载器</title> <script src="jquery-1. ...

  4. Bootstrap系列 -- 24. 下拉菜单基本用法

    在使用Bootstrap框架的下拉菜单时,必须调用Bootstrap框架提供的bootstrap.js文件.当然,如果你使用的是未编译版本,在js文件夹下你能找到一个名为“dropdown.js”的文 ...

  5. GnuDIP制作动态域名服务器(DDNS Server)_转载http://blog.sina.com.cn/s/blog_4d4c23530100rlfj.html

    这个阶段在做DDNS,虽然有dyndns和tzo两个免费的国外的DDNS服务器(支持免费用户注册使用),但是公司需求中要有GnuDIP这种服务.于是只能自己制作DDNS服务器,颇费功夫,于是想把这段记 ...

  6. [bzoj 2431][HAOI2009]逆序对数列(递推+连续和优化)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2431 分析: f(i,j)表示前i个数字逆序对数目为j时候的方案数 那么有f(i,j) ...

  7. Sencha Touch 手机移动开发框架 HTML5 项目压缩方案;

    Sencha Touch框架生成基本项目目录结构 Index.html/ App.js App.json /touch[sdk]/ /Sencha-touch.js /src Resources/ A ...

  8. “耐撕”团队 2016.04.05 站立会议

    1. 时间: 20:10--20:25  共计15分钟. 2. 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:ht ...

  9. BZOJ SCOI2005骑士精神

    裸IDA*,ans从1到15循环来限制搜索深度. #include<cstdio> #include<cstring> #include<algorithm> us ...

  10. Hibernate-清理一级缓存

    Session执行一些sql语句把内存中的对象的状态同步到数据库,这个过程被称为session清理. 在默认情况下,Session会在下面的时间点清理缓存. 1 当应用程序调用net.sf.hiber ...