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

  1. 4
  2. aaaaaaa
  3. baaaaaa
  4. abaaaaa
  5. aabaaaa
  6. 0

Sample Output

The highest possible quality is 1/3.

题意:每两个点之间都有一条带权边,求最小生成树。因为是稠密图, 所以其实是更适合prim的。。但是这题比较松,krusal也过了

一共有n个点, 有n * n / 2 条边 ,(为防止溢出,要写成n / 2 * n),边数写错会RE

因为是无向图,所以注意一下两个for循环的起点,这样边数才是上面的数量,不然万一写成n * n条边又RE了

模板跑一遍,注意这里kruskal返回的是最小生成树的所有边的权值之和

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. typedef long long ll;
  5. using namespace std;
  6. const int maxn = 2000 + 100;//最大点数
  7. const int maxm = maxn / 2 * maxn;//最大边数
  8. int F[maxn];//并查集, F[x]储存x的父节点
  9. int tol;
  10. char a[maxn][7];
  11. struct Edge{
  12. int u, v, w;//储存每条边的两个端点以及边的权值
  13. }edge[maxm];
  14. bool cmp(Edge a, Edge b){
  15. return a.w < b.w;
  16. }
  17. void addedge(int u, int v, int w){//加入最小生成树当中
  18. edge[tol].u = u;
  19. edge[tol].v = v;
  20. edge[tol++].w = w;
  21. }
  22. int find(int x){
  23. if(F[x] == -1) return x;
  24. else return F[x] = find(F[x]);
  25. }
  26. int kruskal(int n){//参数为点的数量
  27. memset(F, -1, sizeof(F));//刚开始每一个点都是一个独立的连通图
  28. int cnt = 0;//已经加入到最小生成树中的边
  29. sort(edge, edge + tol, cmp);
  30. int ans = 0;//最小生成树的边的权值之和
  31. for(int i = 0 ; i < tol; i++){
  32. int u = edge[i].u;
  33. int v = edge[i].v;
  34. int w = edge[i].w;
  35. int t1 = find(u), t2 = find(v);
  36. if(t1 != t2){//u和v不在一个连通图中
  37. ans += w;
  38. F[t1] = t2;
  39. cnt++;
  40. }
  41. if(cnt == n-1) break;
  42. }
  43. if(cnt < n-1) return -1;//不连通
  44. return ans;
  45. }
  46. int getw(int i, int j){
  47. int qq = 0;
  48. for(int k = 0; k <7; k++){
  49. if(a[i][k] != a[j][k])qq++;
  50. }
  51. return qq;
  52. }
  53. int main(){
  54. int n;
  55. //下面建立最小生成树的来源图,即n ^2 /2条边的图
  56. while(scanf("%d", &n) && n){
  57. for(int i = 1; i <= n; i++){
  58. scanf("%s", a[i]);
  59. }
  60. tol = 0;
  61. for(int i = 1; i <= n-1; i++)
  62. for(int j = i; j <= n; j++)addedge(i, j, getw(i, j));
  63. printf("The highest possible quality is 1/%d.\n", kruskal(n));
  64. }
  65. return 0;
  66. }

因为是多组数据,所以memset要放在while里面,不然会WA

POJ 1789 Truck History (Kruskal最小生成树) 模板题的更多相关文章

  1. POJ 1789 Truck History【最小生成树模板题Kruscal】

    题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...

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

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

  3. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  4. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

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

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

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

    模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...

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

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

  8. Kuskal/Prim POJ 1789 Truck History

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

  9. POJ 1789 -- Truck History(Prim)

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

随机推荐

  1. NSOperationQueue队列依赖相关思考

    添加依赖后,队列中网络请求任务有依赖关系时,任务结束判定以数据返回为准还是以发起请求为准? waitUntilFinished方法容易误解. 依赖关系 // // ViewController.m / ...

  2. 还在使用OpenGL ES做渲染,你Out了,赶紧来拥抱Vulkan吧~

    背景介绍 Vulkan是Khronos组织制定的"下一代"开放的图形显示API.是与DirectX12能够匹敌的GPU API标准. Vulkan是基于AMD的Mantle API ...

  3. Ceph 文件系统-全网最炫酷的Ceph Dashboard页面和Ceph监控 -- <5>

    Ceph Dashboard实现 Ceph Dashboard介绍 Ceph 的监控可视化界面方案很多----grafana.Kraken.但是从Luminous开始,Ceph 提供了原生的Dashb ...

  4. 小程序中的pick

    picker:从底部弹起的滚动选择器. 属性:model  string类型  说明:选择器类型 : selector  普通选择器 multiSelector   多列选择器 time   时间选择 ...

  5. 一文搞定Spring Boot + Vue 项目在Linux Mysql环境的部署(强烈建议收藏)

    本文介绍Spring Boot.Vue .Vue Element编写的项目,在Linux下的部署,系统采用Mysql数据库.按照本文进行项目部署,不迷路. 1. 前言 典型的软件开发,经过" ...

  6. Unity_Dungeonize 随机生成迷宫

    本文对随机生成迷宫的实现思路进行记录,其作用在于为游戏过程提供随机性以及节省开发周期,下面是Dungeonize的结构 随机迷宫的生成主要包括几个阶段 1.生成房间体结构,为墙体,自定义房间,自定义物 ...

  7. Window同一电脑配置多个git公钥

    前言 配置多个本地ssh-key之前,先初始化下GIt环境哦! 可以参照:https://www.cnblogs.com/poloyy/p/12185132.html 执行前两步就好啦 本地生成两个s ...

  8. dp——洛谷 P1541 乌龟棋 —— by hyl天梦

    题目:(转自 https://www.luogu.com.cn/problem/P1541) 题目描述 乌龟棋的棋盘是一行NN个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第NN格是 ...

  9. kafka(一)-为什么选择kafka

    作为开发人员,我们在选择一个框架或者工具时,我们都需要考虑些什么,我们不是头脑发热,一拍脑袋就它了,我们首先要认清这个框架或工具的作用是什么,能给我们带来什么样的好处,同时也要考虑带来什么样的负面结果 ...

  10. spring-cloud-gateway报错

    2019-08-13 09:41:19.216 WARN [-,,,] 10084 --- [ main] ConfigServletWebServerApplicationContext : Exc ...