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. 学习笔记——Maven 命令行选项

    2014-10-09:更新裁剪反应堆具体用法 说明: 1.使用-选项时,和后面的参数之间可以不要空格.而使用--选项时,和后面的参数之    间必须有空格.如下面的例子: $ mvn help:des ...

  2. WIN7下USB多点触摸,一次发多个数据包的延迟问题,重要!

    这个问题很常见, 花了差不多一个星期时间来解决.硬件相关的东西太多坑了,而且这些坑不像代码那样可见.   使用混合模式,每次最多报告2个点.如果是5点则需要上报三次. 问题就来了,atmel的CTP最 ...

  3. 【MPI学习3】MPI并行程序设计模式:不同通信模式MPI并行程序的设计

    学习了MPI四种通信模式 及其函数用法: (1)标准通信模式:MPI_SEND (2)缓存通信模式:MPI_BSEND (3)同步通信模式:MPI_SSEND (4)就绪通信模式:MPI_RSEND ...

  4. Java并发之:生产者消费者问题

    生产者消费者问题是Java并发中的常见问题之一,在实现时,一般可以考虑使用juc包下的BlockingQueue接口,至于具体使用哪个类,则就需要根据具体的使用场景具体分析了.本文主要实现一个生产者消 ...

  5. 百度地图 IOS版开发经验分享

    最近刚完成app中地图的应用,感觉非常的曲折,折腾了很久,刚才开始一直不能成功使用百度地图api,报一大堆的错误,后来换到高德地图,又发现服务端使用的百度的经纬度,又从高德换回百度,泪奔. 这里简单比 ...

  6. node 日志

    我们主动控制的log4js,还有一些日志是继承好的,错误日志了,进程日志了,请求日志了. 1. var express = require('express');var logger = requir ...

  7. [BZOJ1271][WC2008]秦腾与教学评估(巧妙的二分)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1271 分析: 很巧妙的一道题 因为最多只有一个点是奇数,所以说明这个点前面的前缀和都是 ...

  8. 浅谈分治算法在OI中的应用

    分治虽然是基本思想,但是OI中不会出裸分治让你一眼看出来,往往都是结合到找规律里面. 先来个简单的: 奇妙变换 (magic.pas/c/cpp) [问题描述]   为了奖励牛牛同学帮妈妈解决了大写中 ...

  9. 第二十三课:jQuery.event.add的原理以及源码解读

    本课主要来讲解一下jQuery是如何实现它的事件系统的. 我们先来看一个问题: 如果有一个表格有100个tr元素,每个都要绑定mouseover/mouseout事件,改成事件代理的方式,可以节省99 ...

  10. 【web必知必会】—— 使用DOM完成属性填充

    本文介绍了使用DOM的简单方法实现动态加载图片的功能. 前文介绍了: 1 DOM四个常用的方法 首先看一下效果,初始时是一个相册,可以点击导航,切换图片,并切换下方显示内容: 点击house,可以动态 ...