题目链接:http://poj.org/problem?id=1789

大意:

不同字符串相同位置上不同字符的数目和是它们之间的差距。求衍生出全部字符串的最小差距。

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<algorithm>
  4. using namespace std;
  5. const int MAXN = ;
  6.  
  7. int cnt;
  8. int pre[MAXN];
  9. char s[MAXN][];
  10.  
  11. struct Edge
  12. {
  13. int from, to;
  14. int val;
  15. }edge[MAXN * (MAXN - ) / ];
  16.  
  17. bool cmp(Edge a, Edge b)
  18. {
  19. return a.val < b.val;
  20. }
  21.  
  22. int find(int x)
  23. {
  24. if(pre[x] == x)
  25. return x;
  26. else
  27. {
  28. int root = find(pre[x]);
  29. pre[x] = root;
  30. return pre[x];
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. int n;
  37. while(scanf("%d", &n) != EOF)
  38. {
  39. if(n == )
  40. break;
  41. getchar();
  42. cnt = ;
  43. for(int i = ; i <= n; i ++)
  44. pre[i] = i;
  45. for(int i = ; i <= n; i ++)
  46. scanf("%s", s[i] + );
  47. for(int i = ; i < n; i ++)
  48. {
  49. for(int j = i + ; j <= n; j ++)
  50. {
  51. int sum = ;
  52. for(int k = ; k <= ; k ++)
  53. if(s[i][k] != s[j][k])
  54. sum ++;
  55. edge[++ cnt].from = i;
  56. edge[cnt].to = j;
  57. edge[cnt].val = sum;
  58. }
  59. }
  60. sort(edge + , edge + + cnt, cmp);
  61. int ans = ;
  62. for(int i = ; i <= cnt; i ++)
  63. {
  64. int xx = find(edge[i].from), yy = find(edge[i].to);
  65. if(xx != yy)
  66. {
  67. pre[yy] = xx;
  68. ans += edge[i].val;
  69. }
  70. }
  71. printf("The highest possible quality is 1/%d.\n", ans);
  72. }
  73. return ;
  74. }

POJ 1789 Truck History【最小生成树模板题Kruscal】的更多相关文章

  1. poj 1789 Truck History 最小生成树

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

  2. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

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

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

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

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

  10. POJ 1789 Truck History (Kruskal)

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

随机推荐

  1. 更改ejs模板引擎的后缀为html

    安装 EJS 在项目目录右键->Open Command Prompt Here 输入 npm install ejs 打开app.js //app.set('view engine', 'ja ...

  2. 002转载----C# 基于OneNet 的物联网数据通信

    作者:lnwin521 来源:CSDN 原文:https://blog.csdn.net/lnwin521/article/details/84549606 (遇到404情况请复制粘贴后再打开)版权声 ...

  3. Shell 06 awk

    一.基本操作方法 ######################################### grep 按行进行查找 vim  编辑文档,交互式 ####################### ...

  4. xgzc— math 专题训练(一)

    Lucas定理 当\(p\)是质数时,有\((^n_m)\equiv(^{n/p}_{m/p}) * (^{n\%p}_{m\%p}) \pmod{p}\) 狄利克雷卷积 定义:\((f*g)(n)= ...

  5. windows问题集合

    1.windows创建内核对象时系统会创建内核数据块,我们通过什么方式去创建,打开,操作这些数据块呢?微软是如何做的?如果是你又会如何做?(提示:内核句柄) 2.进程  发展历史(系统方面发展) 答: ...

  6. Linux操作系统load average过高,kworker占用较多cpu

    Linux操作系统load average过高,kworker占用较多cpu 今天巡检发现,mc1的K8S服务器集群有些异常,负载不太均衡.其中10.2.75.32-34,49的load averag ...

  7. 安装curl依赖库后yum不能使用问题解决

    安装curl 1)  进入/home目录下,解压curl-7.54.0.tar.gz [root@localhost home]# tar zxvf curl-7.54.0.tar.gz 2)  依次 ...

  8. Python——装饰器(Decorator)

    1.什么是装饰器? 装饰器放在一个函数开始定义的地方,它就像一顶帽子一样戴在这个函数的头上.和这个函数绑定在一起.在我们调用这个函数的时候,第一件事并不是执行这个函数,而是将这个函数做为参数传入它头顶 ...

  9. 详解Intellij IDEA中.properties文件中文显示乱码问题的解决

    首先,你可能会见到如下提示: File encoding is disabled because .properties file (see Settings|Editor|File Encoding ...

  10. Build Telemetry for Distributed Services之OpenCensus:C#

    OpenCensus Easily collect telemetry like metrics and distributed traces from your services OpenCensu ...