Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21376   Accepted: 8311

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

  1. The highest possible quality is 1/3.

题目给了N个字符串,每个字符串有7个字符。每个字符串之间都有所谓的“距离”:即不相等的数量。问这些字符串之间一共的距离之和最小是多少。

计算每对字符串之间的距离,之后求其最小生成树。

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. #include <string>
  6. #include <cstring>
  7. #pragma warning(disable:4996)
  8. using namespace std;
  9.  
  10. int num;
  11. char truck[2005][10];
  12. int map[2005][2005];
  13. int stack[2005];
  14. int minidis[2005];
  15.  
  16. int dis(int x,int y)
  17. {
  18. int i,result=0;
  19. for(i=0;i<7;i++)
  20. {
  21. if(truck[x][i]!=truck[y][i])
  22. {
  23. result++;
  24. }
  25. }
  26. return result;
  27. }
  28.  
  29. int prim()
  30. {
  31. int i,j,s,result;
  32.  
  33. memset(stack,0,sizeof(stack));
  34. for(i=1;i<=num;i++)
  35. {
  36. minidis[i]=15;
  37. }
  38.  
  39. stack[1]=1;
  40. minidis[1]=0;
  41. s=1;
  42. result=0;
  43. for(i=1;i<=num-1;i++)
  44. {
  45. int min_all=15;
  46. int min_temp=0;
  47. for(j=2;j<=num;j++)
  48. {
  49. if(stack[j]==0&&minidis[j]>map[s][j])
  50. {
  51. minidis[j]=map[s][j];
  52. }
  53. if(stack[j]==0&&minidis[j]<min_all)
  54. {
  55. min_temp=j;
  56. min_all=minidis[j];
  57. }
  58. }
  59. s=min_temp;
  60. stack[s]=1;
  61. result += min_all;
  62. }
  63. return result;
  64. }
  65.  
  66. int main()
  67. {
  68. int i,j;
  69. while(cin>>num)
  70. {
  71. if(num==0)
  72. break;
  73. for(i=1;i<=num;i++)
  74. {
  75. scanf("%s",truck[i]);
  76. }
  77. for(i=1;i<=num;i++)
  78. {
  79. for(j=i+1;j<=num;j++)
  80. {
  81. map[i][j]=map[j][i]=dis(i,j);
  82. }
  83. }
  84. cout<<"The highest possible quality is 1/"<<prim()<<"."<<endl;
  85. }
  86.  
  87. return 0;
  88. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1789:Truck History的更多相关文章

  1. POJ 1789:Truck History(prim&amp;&amp;最小生成树)

    id=1789">Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17610   ...

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

    题意:距离定义为两个字符串的不同字符的位置个数.然后求出最小生成树. #include <algorithm> #include <cstdio> #include <c ...

  3. poj 1789 prime

    链接:Truck History - POJ 1789 - Virtual Judge  https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...

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

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

  5. Kuskal/Prim POJ 1789 Truck History

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

  6. POJ 1789 Truck History (Kruskal)

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

  7. poj 1789 Truck History

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

  8. POJ 1789 -- Truck History(Prim)

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

  9. poj 1789 Truck History 最小生成树

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

随机推荐

  1. B. Yet Another Crosses Problem

    B. Yet Another Crosses Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. (转)让一个进程启动时Windbg自动Attach上去

    如何让一个进程启动时Windbg自动Attach上去 以IE为例:需要在注册表中创建一项HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\Current ...

  3. Exchange 2003 群集迁移 & SPS 2003 迁移、SQL Server 2000群集

    哈哈,本人自己写的文档,内容太多了,有195页,上传到Blog里面,应该是很难看的,排版也不太好. 记得下载时后改名字,用WinRAR解压合并. 第1章 迁移环境介绍 第2章 共享磁盘柜配置 第3章 ...

  4. C++编程学习(十)引用

    引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字.一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量. 注意以下几点与指针的区别: 不存在空引用.引用必须连接到一块合法的 ...

  5. Windows下C++遍历文件夹中的文件

    Windows下,在VS中开发,C++遍历文件夹下文件. 在Windows下,遍历文件所用到的函数和结构体,需要在程序中包含头文件#include <io.h>,在VS中,头文件io.h实 ...

  6. python 定义一个空集合、空字典

    s = set() #定义一个空集合 s = {} #定义一个空字典

  7. 四十、SAP中CASE语句用法

    一.上代码 二.选择内容 三.输出 四.我们选择一个其他的值 五.查看输出

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring声明式事务管理(基于Annotation注解方式实现)

    在 Spring 中,除了使用基于 XML 的方式可以实现声明式事务管理以外,还可以通过 Annotation 注解的方式实现声明式事务管理. 使用 Annotation 的方式非常简单,只需要在项目 ...

  9. 第十三篇Django Logging配置样例

    第十三篇Django Logging配置样例 阅读目录(Content) Django 日志配置模板 官方链接 Django Logging Django 日志配置模板 LOGGING = { 've ...

  10. ConcurrentHashMap核心源码浅析

    1.引子 并发编程中使用HashMap可能导致程序死循环.因为多线程会put方法添加键值对时将导致HashMap的Entry链表形成环形数据结构,一旦形成环形数据结构,Entry的next节点永远不为 ...