题目:
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 t o is the original type and t d the type derived from it and d(t o,t d) 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.

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX 2005
char str[MAX][8];
int n,dif,k,f[MAX];
struct Node{int x,y,weight;}node[MAX * MAX];
int find_father(int x)
{int r = x,temp;while(r != f[r]) r = f[r];while(x != f[x]){temp = f[x];f[x] = r;x = temp;}return x;
}
void combine(int a,int b)
{int c = find_father(a);int d = find_father(b);if(c == d) return;f[d] = c;
}
int kruskal(int m)
{int ans = 0,num = 0;for(int i = 0;i < m;i++){if(find_father(node[i].x) != find_father(node[i].y)){combine(node[i].x,node[i].y);ans += node[i].weight;num++;}}return ans;
}
bool cmp(Node a,Node b) {return a.weight < b.weight;}
int distance(char a[],char b[])
{int sum = 0;for(int i = 0;i < 7;i++){if(a[i] != b[i]) sum++;}return sum;
}
int main()
{int ans;while(~scanf("%d",&n)){if(n == 0) break;k = 0;for(int i = 0;i < n;i++){scanf("%s",str[i]);f[i] = i;}for(int i = 0;i < n - 1;i++){for(int j = i + 1;j < n;j++){dif = distance(str[i],str[j]);node[k].x = i;node[k].y = j;node[k++].weight = dif;}}sort(node,node + k,cmp);ans = kruskal(k);cout << "The highest possible quality is 1/" << ans << "." << endl;}return 0;
}

本题实质上是以任意两个字符串为结点,以字符串字母不同位置的个数为两节点之间边的权值,求解最小生成树。这里用kruskal算法比较方便。

J - Truck History相关推荐

  1. Kuskal/Prim POJ 1789 Truck History

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

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

    链接: http://poj.org/problem?id=1789 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2213 ...

  3. 【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)

    题干: Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used f ...

  4. Truck History prime

    题目表述: Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetab ...

  5. POJ 1789 Truck History

    最小生成树问题. 给你一组字母序列,问你最有可能的演变,也就是把全部的序列连通所花费最小. 每次派生的花费 取决于两个字符串上 不同的字母个数. 于是两两算出花费,然后Kruskal算最小. #inc ...

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

    题目:http://poj.org/problem?id=1789 大意:每个卡车都有自己的编号,由七位字母组成 d(to,td) is the distance of the types指t0 和 ...

  7. POJ-1789 Truck History 最小生成树

    计算整个汽车演化过程中所要改变的最少的字符数.改变的字符数的计算方式为1-7位不同位的个数. 代码如下: #include <cstdlib> #include <cstring&g ...

  8. 【POJ】【最小生成树】1789 Truck History

    1 思路 题目链接. 最小生成树(MST)问题. 2 代码 代码来自宇宙吾心博客. #include <iostream> #include <limits> #include ...

  9. poj1789 Truck History(最小生成树)

    2018-3-24 简单的最小生成树问题. 题目大意是: 给你n个字符串,他们的distance就是串中不同字符的个数,要求算出所有串的distance's 最小 sum : #include< ...

最新文章

  1. SQL Server Management Studio消失了
  2. html改元素怎么保存,是否可以在NW.js中保存html元素更改?
  3. Git笔记(5) 状态记录
  4. java字符替换函数示例_Java字符串替换函数replace()用法解析
  5. C#实战篇-基于OOP设计新体育彩票选号器
  6. 可信任的人工智能(一)
  7. VMWARE平台STS证书过期
  8. 联想电脑BIOS失败教程
  9. 快速学会关键路径、最早开始时间和最晚开始时间的计算
  10. 小白简单式安装VM虚拟机
  11. 移动web适配的方式
  12. “正话反说”:A和B在玩一个游戏,两人轮流说一句话,这句话正读反读都一样,如adgda,谁先说错,谁出局,另一个人胜出。编写一个函数用于判断这句话是否符合要求,符合要求时,函数返回1,否则函数返回0
  13. 如何在PPT中用文本框制作海报?这个方法很简单,不信来看
  14. 【转载】VLOOKUP函数多条件查询 VLOOKUP三个条件查询
  15. 一文看懂25个神经网络模型,神经网络模型结构图
  16. python推箱子游戏顶层设计子层设计_python实现推箱子游戏
  17. 熬了整整30天,搭建kafka服务器centos8
  18. SAP VL02N为某个交货单PGI报错说HU和序列号里的标识符不一致问题之对策II
  19. 方向向量转欧拉角_【姿态表示】旋转向量、旋转矩阵、欧拉角、四元数
  20. 周易Java_关于维度、计算机、周易的漫思

热门文章

  1. MySQL索引重点问题总结(需要完整脑图的联系我)
  2. 计算机简史:从分布式到中心化的博弈螺旋
  3. 有计算机考试励志的文案,抖音冲刺高考励志文案
  4. 微信聊天记录服务器端口是什么意思,微信端口异常是什么意思?
  5. 女程序员在IT界的发展方向是什么?
  6. Mesos/Omega/Borg(K8S)/Firemament对比
  7. 奥运会倒计时,老蒋作品,
  8. php中对数组进行转码,PHP 数组转码
  9. 一天 第一章 花在花苞未有泪,人见不识无言对
  10. 关于植物大战僵尸如何更改关卡与金币