题目链接:POJ 1789

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/Σ_{(t_o,t_d)}d(t_o,t_d)\)

where the sum goes over all pairs of types in the derivation plan such that \(t_o\) is the original type and \(t_d\) the type derived from it and d(\(t_o\),\(t_d\)) 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

CTU Open 2003

Solution

题意

用一个 \(7\) 位的字符串代表一个编号,两个编号之间的距离等于这两个编号之间不同字母的个数。

给定 \(n\) 个编号,求连接所有编号的最短距离。

思路

Kruskal

把每个字符串看成结点,用无向边连接任意两个结点,边权为两个字符串之间的距离,对构成的无向图求最小生成树就是答案。

Code

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 2010, M = 4e6 + 10;
const int inf = 0x3f3f3f3f;
int n, m;
int ans; struct Edge {
int x, y, z;
} edge[M]; int fa[N]; int cmp(Edge a, Edge b) {
return a.z < b.z;
} int get(int x) {
if(x == fa[x]) return x;
return fa[x] = get(fa[x]);
} void init() {
for(int i = 0; i <= n; ++i) {
fa[i] = i;
}
ans = 0;
} void kruskal() {
sort(edge + 1, edge + 1 + m, cmp);
for(int i = 1; i <= m; ++i) {
int x = get(edge[i].x);
int y = get(edge[i].y);
if(x != y) {
ans += edge[i].z;
fa[x] = y;
}
}
} char str[N][10]; int dis(int x, int y) {
int res = 0;
for(int i = 0; i < 7; ++i) {
if(str[x][i] != str[y][i]) {
++res;
}
}
return res;
} int main() {
while(scanf("%d", &n) && n) {
for(int i = 1; i <= n; ++i) {
scanf("%s", str[i]);
}
init();
m = 0;
for(int i = 1; i <= n; ++i) {
for(int j = i + 1; j <= n; ++j) {
edge[++m].x = i;
edge[m].y = j;
edge[m].z = dis(i, j);
}
}
kruskal();
printf("The highest possible quality is 1/%d.\n", ans);
}
return 0;
}

POJ 1789 Truck History (Kruskal)的更多相关文章

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

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

  2. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  3. Kuskal/Prim POJ 1789 Truck History

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

  4. POJ 1789 -- Truck History(Prim)

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

  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 最小生成树

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

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

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

随机推荐

  1. AtCoder ABC 140E Second Sum

    题目链接:https://atcoder.jp/contests/abc140/tasks/abc140_e 题目大意 给定一个 1~N 的排列 P. 定义$X_{L, R}$的值为$P_L, P_{ ...

  2. redis的set()方法参数

    redis 127.0.0.1:6379> SET KEY VALUE [EX seconds] [PX milliseconds] [NX|XX]   EX seconds − 设置指定的到期 ...

  3. 50-python基础-python3-列表-函数sorted() 对列表进行临时排序

    sorted()函数对列表进行临时排序,返回排序后的列表: 区别列表方法sort()原地修改,无返回值. 1-要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted() . 2 ...

  4. windows 如何配置 Go 环境(Zip archive 方式)?

    windows 如何配置 Go 环境(Zip archive 方式)? 下载地址:https://dl.google.com/go/go1.12.5.windows-amd64.zip 解压 go1. ...

  5. 【转】linux下使用man查看C函数用法

    大家都知道在Unix/Linux中有个man命令,可以查询常用的命令,函数.可是对于我们这样只知道用"man 函数名"来查询的人来说,会遇到很多问题,比如: man read,我想 ...

  6. Java8 Stream流API常用操作

    Java版本现在已经发布到JDK13了,目前公司还是用的JDK8,还是有必要了解一些JDK8的新特性的,例如优雅判空的Optional类,操作集合的Stream流,函数式编程等等;这里就按操作例举一些 ...

  7. c# 编程--方法(函数)

    方法(函数) 能够独立完成某项功能的模块    函数的四要素:函数名.输入.输出.函数体    函数定义.函数的调用 函数就是将一堆代码进行重用的一种机制,函数就是一段代码,这段代码可能有输入的值(参 ...

  8. elasticsearch 英文数字组合字符串模糊检索

    不分词,然后用wildcard查询 { "query": { "wildcard": { "字段名": "*123*" ...

  9. redis出现MISCONF Redis is configured to save RDB snapshots...的错误

    今天重启服务器在连接redis数据库时突然报错: MISCONF Redis is configured to save RDB snapshots, but it is currently not ...

  10. 第二章 Kubernetes pod状态问题

    一.ImagePullBackOff 当我们创建一个名字为myapp的deployment的时候,它指向的是一个不存在的docker镜像: 最常见的有两个问题: (a)指定了错误的容器镜像 (b)使用 ...