题意:距离定义为两个字符串的不同字符的位置个数。然后求出最小生成树。

  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. const int N=;
  6. const int M=;
  7. char code[N][];
  8. int f[N];//并查集
  9. struct edge{
  10. int u,v,w;
  11. }e[M];
  12. int n,tot;
  13. void add(int u,int v,int w){
  14. e[tot].u=u;e[tot].v=v;e[tot++].w=w;
  15. }
  16. bool cmp(edge a,edge b){
  17. return a.w<b.w;
  18. }
  19. int find(int x){
  20. if(f[x]==-)return x;
  21. return f[x]=find(f[x]);
  22. }
  23. int Kruskal(){
  24. memset(f,-,sizeof f);
  25. sort(e,e+tot,cmp);
  26. int cnt=,ans=;
  27. for(int i=;i<tot;i++){
  28. int u=e[i].u,v=e[i].v,w=e[i].w;
  29. int fu=find(u),fv=find(v);
  30. if(fu!=fv){
  31. ans+=w;
  32. f[fu]=fv;
  33. cnt++;
  34. }
  35. if(cnt==n-)break;
  36. }
  37. return ans;
  38. }
  39. void solve(){
  40. for(int i=;i<=n;i++)
  41. for(int j=i+;j<=n;j++)
  42. {
  43. int dis=;
  44. for(int k=;k<;k++)
  45. if(code[i][k]!=code[j][k])dis++;
  46. add(i,j,dis);
  47. }
  48. }
  49. int main(){
  50. while(scanf("%d ",&n),n){
  51. tot=;
  52. for(int i = ; i <= n; i++)
  53. gets(code[i]);
  54. solve();
  55. printf("The highest possible quality is 1/%d.\n", Kruskal());
  56. }
  57. return ;
  58. }

  

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

  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. POJ 1789 -- Truck History(Prim)

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

  4. Kuskal/Prim POJ 1789 Truck History

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

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

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

  9. POJ 1789 Truck History (Kruskal)

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

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

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

随机推荐

  1. js点击左右滚动+默认自动滚动类

    js点击左右滚动+默认自动滚动类 点击下载

  2. Centos 检查磁盘读写性能

    启动Tomcat发现deploy war的速度明显变慢, 怀疑磁盘出问题 测试写入 [tomcat@localhost ~]$ dd if=/dev/zero of=kwxgd bs=64k coun ...

  3. Location of several networks in brain

    Source: Naci, L., et al. (2014). "A common neural code for similar conscious experiences in dif ...

  4. css 九宫格

    http://jsfiddle.net/webtiki/kQXkt/ http://jsfiddle.net/webtiki/MpXYr/3/embedded/ https://www.web-tin ...

  5. 【转】如何利用logrotate工具自动切分滚动中的日志文件

    FROM : http://www.2cto.com/os/201503/381812.html 在很多实际项目中,应用程序会持续写日志,如果程序代码中没有调用支持自动切分(如按filesize或da ...

  6. distributed caching for .net applications

    distributed caching for .net applications fast, scalable distributed caching with meaningful perform ...

  7. Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)

    在前面的文章中介绍的了如何使用Cordova进行跨平台应用的开发,使用Cordova的话基本上就不需要在写系统原生代码了,只要通过编写html页面和js方法即可. 但在有些特殊情况下,还是是需要htm ...

  8. noi题库(noi.openjudge.cn) 1.8编程基础之多维数组T21——T25

    T21 二维数组右上左下遍历 描述 给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按从左上到右下的对角线顺序遍历整个数组. 输入 输入的第一行上有两个整数,依次为 ...

  9. hadoop 2.6全分布安装

    环境:centos 6.6 + hadoop2.6 虚拟机:(vmware fusion 7.0.0) 虚拟机hostname      /    IP地址 master / 192.168.187. ...

  10. oracle: job使用

    oracle的job,实际上就是数据库内置的定时任务,类似代码中的Timer功能.下面是使用过程: 这里我们模拟一个场景:定时调用存储过程P_TEST_JOB 向表TEST_JOB_LOG中插入数据 ...