题目连接

http://poj.org/problem?id=1789

Truck History

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

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

堆优化的Prim最小生成树算法。。

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<vector>
  7. #include<queue>
  8. #include<map>
  9. using std::map;
  10. using std::min;
  11. using std::find;
  12. using std::pair;
  13. using std::vector;
  14. using std::multimap;
  15. using std::priority_queue;
  16. #define pb(e) push_back(e)
  17. #define sz(c) (int)(c).size()
  18. #define mp(a, b) make_pair(a, b)
  19. #define all(c) (c).begin(), (c).end()
  20. #define iter(c) __typeof((c).begin())
  21. #define cls(arr, val) memset(arr, val, sizeof(arr))
  22. #define cpresent(c, e) (find(all(c), (e)) != (c).end())
  23. #define rep(i, n) for(int i = 0; i < (int)n; i++)
  24. #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
  25. const int N = 2100;
  26. const int INF = 0x3f3f3f3f;
  27. struct P {
  28. int w, v;
  29. P(int i = 0, int j = 0) :w(i), v(j) {}
  30. inline bool operator<(const P &x) const {
  31. return w > x.w;
  32. }
  33. };
  34. struct Prim {
  35. typedef char State[8];
  36. struct edge { int to, w, next; }G[N * N];
  37. State st[N];
  38. bool vis[N];
  39. int tot, head[N], mincost[N];
  40. inline void init() {
  41. tot = 0, cls(vis, false), cls(head, -1), cls(mincost, 0x3f);
  42. }
  43. inline void add_edge(int u, int v, int w) {
  44. G[tot] = (edge){ v, w, head[u] }; head[u] = tot++;
  45. }
  46. inline int calc(int i, int j) {
  47. int res = 0;
  48. rep(k, 7) {
  49. if(st[i][k] != st[j][k]) res++;
  50. }
  51. return res;
  52. }
  53. inline void built(int n) {
  54. rep(i, n) scanf("%s", st[i]);
  55. rep(i, n) {
  56. rep(j, n) {
  57. int ret = calc(i, j);
  58. if(i == j) continue;
  59. add_edge(i + 1, j + 1, ret);
  60. }
  61. }
  62. }
  63. inline void prim(int s) {
  64. int ans = 0;
  65. priority_queue<P> q;
  66. q.push(P(0, s));
  67. for(int i = head[s]; ~i; i = G[i].next) {
  68. mincost[G[i].to] = G[i].w;
  69. q.push(P(G[i].w, G[i].to));
  70. }
  71. mincost[s] = 0, vis[s] = true;
  72. while(!q.empty()) {
  73. P t = q.top(); q.pop();
  74. int u = t.v;
  75. if(vis[u]) continue;
  76. vis[u] = true;
  77. ans += t.w;
  78. for(int i = head[u]; ~i; i = G[i].next) {
  79. int &d = mincost[G[i].to];
  80. if(d > G[i].w && !vis[G[i].to]) {
  81. d = G[i].w;
  82. q.push(P(G[i].w, G[i].to));
  83. }
  84. }
  85. }
  86. printf("The highest possible quality is 1/%d.\n", ans);
  87. }
  88. inline void solve(int n) {
  89. init(), built(n), prim(1);
  90. }
  91. }go;
  92. int main() {
  93. #ifdef LOCAL
  94. freopen("in.txt", "r", stdin);
  95. freopen("out.txt", "w+", stdout);
  96. #endif
  97. int n;
  98. while(~scanf("%d", &n), n) {
  99. go.solve(n);
  100. }
  101. return 0;
  102. }

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 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 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

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

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

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

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

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

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

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

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

随机推荐

  1. 关于Flash Builder

    ASDoc路径: X:\Program Files\Adobe\Adobe Flash Builder 4.7 (64 Bit)\eclipse\plugins\com.adobe.flexbuild ...

  2. Flash Builder 4.6 BUG 远程访问受阻

    今天调试项目的时候,惊讶的发现在使用RemoteObject进行远程访问时出现奇怪现象,只能在服务器本地实现访问,在其他客户机上提示2048错误,send failed,差点没把我吓死,记得之前测试过 ...

  3. 慕课网-安卓工程师初养成-2-7 Java中变量的使用规则

    来源:http://www.imooc.com/code/1242 不得不接受的变量小脾气: 1.Java 中的变量需要先声明后使用 2.变量使用时,可以声明变量的同时进行初始化,也可以先声明后赋值 ...

  4. 也谈LBP

    LBP(local banary patter)是一种非常经典的用来描述图像局部纹理特征的算子. 1,基本LBP LBP方法自1994年提出,此后就作为一个有效的纹理特征,不断的被人使用和改进.LBP ...

  5. rel=nofollow

    nofollow是什么意思? nofollow是html标签的一个属性值,Google推荐使用nofollow,告诉机器(爬虫)无需追踪目标页,是指禁止蜘蛛爬行和传递权重,但是如果你是通过sitema ...

  6. shell script 基本语法

    几个符号的意义$#:代表后接的参数『个数』,以上表为例这裡显示为『 4 』:$@:代表『 "$1" "$2" "$3" "$4&q ...

  7. Dynamic view

    Views are a useful feature of SQL databases, letting us create virtual tables based on SQL select st ...

  8. Entity Framework with NOLOCK

    在SqlServer中,频繁在同一个数据库表同时进行读写的时候,会存在锁的问题,也就是在前一个insert.update.delete事务操作完毕之前,你不能进行读取,必须要等到操作完毕,你才能进行s ...

  9. [leetcode]_Balanced Binary Tree

    第四道树题,逐渐能写递归了.虽然最后AC的代码还是看了网络,但是距离成功攻克此类问题仅一步之遥. 题目:一棵树,判断是否为AVL.(AVL条件:树上任意一点的左右子树的高度差<=1) 思路:树依 ...

  10. 在c#中使用mongo-csharp-driver操作mongodb

    1)下载安装 下载地址:http://github.com/mongodb/mongo-csharp-driver/downloads 编译之后得到两个dll MongoDB.Driver.dll:顾 ...