链接:

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 = 2005;
const int INF = 0xfffffff;int n;
int J[N][N], dist[N];
char s[N][10];
bool vis[N];int Len(char a[], char b[])
{int ans=0;for(int i=0; i<7; i++){if(a[i]!=b[i])ans++;}return ans;
}int Prim()
{int i, j, ans=0;memset(vis, 0, sizeof(vis));vis[1]=1;dist[1]=0;for(i=1; i<=n; i++)dist[i]=J[1][i];for(i=1; i<n; i++){int MIN=INF;int index=1;for(j=1; j<=n; j++){if(!vis[j] && dist[j]<MIN){index=j;MIN=dist[j];}}vis[index]=1;if(MIN==INF)break;ans += MIN;for(j=1; j<=n; j++){if(!vis[j] && dist[j]>J[index][j] && J[index][j]>0 )dist[j]=J[index][j];}}return ans;
}int main ()
{while(scanf("%d", &n), n){int i, j;memset(s, 0, sizeof(s));for(i=1; i<=n; i++)scanf("%s", s[i]);for(i=1; i<=n; i++)for(j=1; 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 0;
}

转载于:https://www.cnblogs.com/YY56/p/4735082.html

(最小生成树)Truck History --POJ -- 1789相关推荐

  1. Kuskal/Prim POJ 1789 Truck History

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

  2. poj 1789 Truck History(最小生成树 prim)

    题目:http://poj.org/problem?id=1789 大意:每个卡车都有自己的编号,由七位字母组成 d(to,td) is the distance of the types指t0 和 ...

  3. 【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)

    题干: Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used f ...

  4. 【POJ】【最小生成树】1789 Truck History

    1 思路 题目链接. 最小生成树(MST)问题. 2 代码 代码来自宇宙吾心博客. #include <iostream> #include <limits> #include ...

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

    https://vjudge.net/problem/POJ-1789 题意 n个车牌,长度固定为7,车牌与车牌间的距离为不同字母个数.问所有车牌形成一棵树的最小边权和是多少. 分析 最小生成树模板 ...

  6. POJ 1789 Truck History

    最小生成树问题. 给你一组字母序列,问你最有可能的演变,也就是把全部的序列连通所花费最小. 每次派生的花费 取决于两个字符串上 不同的字母个数. 于是两两算出花费,然后Kruskal算最小. #inc ...

  7. poj 1789 TruckHistory 最小生成树 Kruskal、Prim

    题意: n个车牌号,刚开始只有一个车牌,其他车牌都是由一个车牌直接或间接产生,一个车牌到另一个车牌的产生权值是它们之间的数字不同的个数,问产生的最小的边权和,即求最小生成树. Input: 多组数据. ...

  8. POJ-1789 Truck History 最小生成树

    计算整个汽车演化过程中所要改变的最少的字符数.改变的字符数的计算方式为1-7位不同位的个数. 代码如下: #include <cstdlib> #include <cstring&g ...

  9. poj1789 Truck History(最小生成树)

    2018-3-24 简单的最小生成树问题. 题目大意是: 给你n个字符串,他们的distance就是串中不同字符的个数,要求算出所有串的distance's 最小 sum : #include< ...

最新文章

  1. 如何找寻最适合的WEB应用安全解决方案(视频)
  2. SSL剥离工具sslstrip
  3. python无法启动该程序因为计算机中丢失_python报错:无法启动此程序,因为计算机中丢失...
  4. ExtAspNet v3.1.1
  5. 编程获取linuxservercpu、内存和磁盘使用
  6. ceph docker mysql_使用Docker部署单机版Ceph
  7. 为XNA制做安装程序(四)WIX Toolset 3.0 for Visual Studio 2008
  8. 美国发布新的安全备忘录,提升关键基础设施的网络安全
  9. 配置 MAC地址表实现绑定和过滤
  10. 【WH】MVC数据分页扩展类
  11. jmeter-正则表达式提取器
  12. 电商后台管理系统——权限管理模块
  13. python合并大量ts文件_python爬取基于m3u8协议的ts文件并合并
  14. 正则表达式过滤联系方式,微信手机号QQ等
  15. love2d 1. 入门
  16. 微信公众号系列之测试号使用
  17. linux命令之journalctl —— 筑梦之路
  18. Shell ifs 用法
  19. javaweb mysql 连接池 c3p0 配置_javaWeb_JDBC_c3p0数据库连接池
  20. 夯实C++基础学习笔记

热门文章

  1. python调用shell命令-用Python调用Shell命令
  2. python3.5怎么安装pip-在python3.5中使用pip
  3. python爬虫入门教程-Python爬虫入门教程——爬取自己的博客园博客
  4. python安装步骤win10-教你如何在Win10系统安装Python?
  5. python代码示例下载-Python下载网易云歌单歌曲的示例代码
  6. python详细安装教程3.7.4-Linux下Python3.7.4 安装教程
  7. 学python需要什么文化基础-中国大学MOOC的APP2020Python编程基础答案
  8. python好学嘛-python语言好学吗
  9. python爬虫项目-32个Python爬虫实战项目,满足你的项目慌
  10. python图标-python实现的简版iconv