最小生成树,主要是题目比较难懂。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int Maxn=+;
  8. const int maxn=*+;
  9. int Father[Maxn];
  10. struct Edge
  11. {
  12. int from,to,w;
  13. }edge[maxn];
  14. int n,tot;
  15. char s[maxn][];
  16.  
  17. int Find(int x)
  18. {
  19. if(x!=Father[x]) Father[x]=Find(Father[x]);
  20. return Father[x];
  21. }
  22.  
  23. void init()
  24. {
  25. for(int i=;i<=n;i++) Father[i]=i;
  26. tot=;
  27. }
  28.  
  29. bool cmp(const Edge&a,const Edge&b)
  30. {
  31. return a.w<b.w;
  32. }
  33.  
  34. int main()
  35. {
  36. freopen("in.txt","r",stdin);
  37. while(~scanf("%d",&n)){
  38. if(!n) break;
  39. init();
  40. for(int i=;i<n;i++)
  41. scanf("%s",s[i]);
  42.  
  43. for(int i=;i<n;i++)
  44. for(int j=i+;j<n;j++)
  45. {
  46. edge[tot].from=i;
  47. edge[tot].to=j;
  48. edge[tot].w=;
  49. for(int k=;k<;k++)
  50. if(s[i][k]!=s[j][k])
  51. edge[tot].w++;
  52. tot++;
  53. }
  54.  
  55. sort(edge,edge+tot,cmp);
  56.  
  57. int W=;
  58. for(int i=;i<tot;i++)
  59. {
  60. int Fu=Find(edge[i].from);
  61. int Fv=Find(edge[i].to);
  62. if(Fu!=Fv)
  63. {
  64. Father[Fu]=Fv;
  65. W=W+edge[i].w;
  66. }
  67. }
  68. printf("The highest possible quality is 1/%d.\n",W);}
  69. return ;
  70. }

ZOJ 2158 POJ 1789 Truck History的更多相关文章

  1. Kuskal/Prim POJ 1789 Truck History

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

  2. POJ 1789 -- Truck History(Prim)

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

  3. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

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

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

  5. POJ 1789 Truck History (Kruskal)

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

  6. poj 1789 Truck History 最小生成树

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

  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 最小生成树 prim 难度:0

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

随机推荐

  1. layer1.8UI

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  2. json 多重嵌套反序列化和序列化

    namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)      ...

  3. Comparer<T> IComparer<T> IComparable<T>

    Comparer<T>.Default Property Comparer<T>.Default doesn't use your FooComparer class. It ...

  4. ie6的png24问题

    解决IE6的PNG透明JS插件 DD_belatedPNG 引:http://www.cnblogs.com/cobby/archive/2012/05/11/2495801.html IE6的PNG ...

  5. sql优化方式-转载

    我始终认为,一个系统的性能的提高,不单单是试运行或者维护阶段的性能调优的任务,也不单单是开发阶段的事情,而是在整个软件生命周期都需要注意,进行有效工作才能达到的.所以我希望按照软件生命周期的不同阶段来 ...

  6. Debian搭建PPTPD

    先安装pptpd: apt-get install pptpd 编辑 /etc/pptpd.conf #取消下面两行的注释,在文件的底部. # localip 192.168.0.1 # remote ...

  7. Ubuntu系统上安装搜狗拼音输入法sogou

    1. 首先在百度搜索搜狗拼音输入法Linux就能看到它的官网:http://pinyin.sogou.com/linux/ 下载后直接安装.安装成功后在控制台输入 im-config 如果选中的是fc ...

  8. struts2防止重复提交的标签

    struts2 token 使用说明 --------------------------------------------------------------------------------- ...

  9. 3DTouch--2

    苹果太贱了! 3D Touch 只能在真机上面试,模拟器没办法玩! ------------- 描述有点粗燥.....有6S 在手上玩得童鞋会更加清楚,只有玩过才更加体验到. 首先 有几个要知道的手势 ...

  10. jQuery 截取double数据 重新赋值

    $('.prioritySort').each(function(i){ $(this).text($(this).text().substring(0,$(this).text().indexOf( ...