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. Chrome扩展开发之三——Chrome扩展中的数据本地存储和下载

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  2. Mininet的安装与卸载

    1.Mininet的卸载比较简单,只需要执行以下命令: sudo rm -rf /usr/local/bin/mn /usr/local/bin/mnexec /usr/local/lib/pytho ...

  3. C# 6.0部分新特性

    Struct的默认构造函数和属性赋值 我看C# 6 introduce 提到这个功能.但vs2015搭载的NET4.6貌似还不支持这个.所以也不好判断. 属性赋值 /// <summary> ...

  4. [BZOJ1856][SCOI2010]字符串(组合数学)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1856 分析:http://www.cnblogs.com/jianglangcaiji ...

  5. AJAX(一)AJAX的简介和基础

    本节简介(异步链接服务器对象)XMLHTTPRequest以及AJAX的简介. AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML). AJ ...

  6. php字符串比较函数

    比较两个字符串是否相等,最常见的方法就是使用“===”来判断,至于它和“==”的区别,简单来说就是前者强调“identical”类型也要求 一样:后者要求“equal”,值相同就可以了,参考[1].或 ...

  7. 每天一个linux命令(33):ps命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  8. Android Studio快速添加Gson以及GsonFormat的使用

    目录: 一.Android Studio快速添加Gson 二.Android Studio中GsonFormat的使用 三.在线JSON校验格式化工具 一.Android Studio快速添加Gson ...

  9. Android Fresco (Facebook开源的图片加载管理库)

    Fresco是Facebook开源的一个图片加载和管理库. 这里是Fresco的GitHub网址. 同类型的开源库市面有非常多,比如Picasso, Universal Image Loader, G ...

  10. 【转】pageX、clientX、screenX、offsetX、layerX、x

    参考:http://www.cnblogs.com/xesam/archive/2011/12/08/2280509.html chrome: e.pageX——相对整个页面的坐标e.layerX—— ...