链接:

http://poj.org/problem?id=1789

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22133   Accepted: 8581

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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0xfffffff; int n;
int J[N][N], dist[N];
char s[N][];
bool vis[N]; int Len(char a[], char b[])
{
int ans=; for(int i=; i<; i++)
{
if(a[i]!=b[i])
ans++;
}
return ans;
} int Prim()
{
int i, j, ans=; memset(vis, , sizeof(vis));
vis[]=; dist[]=;
for(i=; i<=n; i++)
dist[i]=J[][i]; for(i=; i<n; i++)
{
int MIN=INF;
int index=;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
index=j;
MIN=dist[j];
}
}
vis[index]=;
if(MIN==INF)
break;
ans += MIN; for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]>J[index][j] && J[index][j]> )
dist[j]=J[index][j];
}
} return ans;
} int main ()
{ while(scanf("%d", &n), n)
{
int i, j; memset(s, , sizeof(s));
for(i=; i<=n; i++)
scanf("%s", s[i]); for(i=; i<=n; i++)
for(j=; j<=i; j++)
{
J[i][j]=J[j][i]=Len(s[i], s[j]);
} int ans=Prim();
printf("The highest possible quality is 1/%d.\n", ans);
}
return ;
}

(最小生成树)Truck History --POJ -- 1789的更多相关文章

  1. Truck History - poj 1789 (Prim 算法)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Ad ...

  2. Truck History POJ - 1789

    题目链接:https://vjudge.net/problem/POJ-1789 思路: 题目意思就是说,给定一些长度为7的字符串,可以把字符串抽象为一个点, 每个点之间的距离就是他们本身字符串与其他 ...

  3. F - Truck History - poj 1789

    有一个汽车公司有很多年的汽车制造历史,所以他们会有很多的车型,现在有一些历史学者来研究他们的历史,发现他们的汽车编号很有意思都是有7个小写字母组成的,而且这些小写字母具有一些特别的意义,比如说一个汽车 ...

  4. Truck History POJ - 1789 板子题

    #include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> u ...

  5. poj 1789 prime

    链接:Truck History - POJ 1789 - Virtual Judge  https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...

  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(prim&amp;&amp;最小生成树)

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

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

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

  9. poj 1789 Truck History 最小生成树

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

随机推荐

  1. Haskell语言学习笔记(42)Bifunctor

    Bifunctor class Bifunctor p where bimap :: (a -> b) -> (c -> d) -> p a c -> p b d bim ...

  2. ASP.NET MVC 项目文件夹结构

    首先,打开Visual Studio, 新建一个demo 项目的solution,选择 Blank Soution. 第二步,创建文件夹,按自己的需求创建.在这个Demo 中,我将创建4个文件夹. P ...

  3. spark性能调优 数据倾斜 内存不足 oom解决办法

    [重要] Spark性能调优——扩展篇 : http://blog.csdn.net/zdy0_2004/article/details/51705043

  4. JDBC远程连接数据库

    使用jdbc远程连接数据库(非本地数据库)出现的问题可用的解决方法: 1.修改mysql配置文件 配置文件中注释掉 bind-address=127.0.0.1 2.修改数据库 登入mysql后,更改 ...

  5. hdoj1087 (DP--LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:这题很简单了,纯LIS的解法,没有一点变形,由于数据小,使用O(n^2)LIS解法就足够了 ...

  6. 兼容多浏览器的网页复制插件(ZeroClipboard)

    前言: 常规利用JS编写的网页复制功能是最简单的方法,但是只对IE有效,无法做到兼容其它浏览器,对其他浏览器也就只能弹窗提示用户手动复制了. <script type="text/ja ...

  7. 18-javaweb-ssm 开发中错误总结

    由于web课设于是,写了几天的javaweb,在写的过程中总会遇到奇奇怪怪的一些bug, 一般都得花很多时间解决. 但是解决多了,后面碰到类似的简单多了. 总结下: 一.前端错误: 1.js错误,看前 ...

  8. metasploit 利用MS08-067渗透攻击xp系统全过程

    工具:metasploit,目标系统 windows xp sp3 English 渗透攻击过程 1.search MS08-067 2. use  exploit/windows/smb/ms08_ ...

  9. Golang之go 命令用法

    Go 命令 Go 命令 Go语言自带有一套完整的命令操作工具,你可以通过在命令行中执行go来查看它们: 图1.3 Go命令显示详细的信息 这些命令对于我们平时编写的代码非常有用,接下来就让我们了解一些 ...

  10. 面向对象设计模式纵横谈:Adapter 适配器模式(笔记记录)

    适配(转换)的概念无处不在 适配,即在不改变原有实现的基础上,将原先不兼容的接口转换为兼容的接口.生活中适配转换的例子太多了,也是设计模式里面比较容易理解的一个模式. 动机(Motivation) 在 ...