zoj 2158 Truck History(最小生成树))
来源:互联网 发布:知乎体格式 编辑:程序博客网 时间:2024/06/05 19:59
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
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 Specification
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 Specification
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
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample Output
The highest possible quality is 1/3.题意:有n辆卡车,卡车的编号由7为小写字母的字符串组成,优劣值的计算方法是:将卡车类型编号的距离定义成卡车类型编码字符串中(7个位置上)不同字符串的位置数目。
要使优劣值最大,那么其距离和应该最小,把每个字符串看成一个顶点,距离和看成边长,那么就是求最小生成树。
#include <iostream>#include <algorithm>using namespace std;int Edge[2010][2010];int lowcost[2010];int n;char num[2010][10];int prim(){ int sum=0; for (int i=0; i<n; i++) { lowcost[i]=Edge[0][i]; } lowcost[0]=-1; int k=0; for (int i=1; i<n; i++) { int min=100000; for (int j=0; j<n; j++) { if(lowcost[j]!=-1 && lowcost[j]<min) { min=lowcost[j]; k=j; } } sum+=min; lowcost[k]=-1; for (int j=0;j<n; j++) { if(Edge[k][j]<lowcost[j]) { lowcost[j]=Edge[k][j]; } } } return sum;}int main(){ while (cin>>n && n) { for (int i=0; i<n; i++) { cin>>num[i]; } for (int i=0; i<n; i++) { for (int j=i+1; j<n; j++) { Edge[i][j]=0; int ans=0; //权值 for (int k=0; k<7; k++) { if(num[i][k]!=num[j][k]) { ans++; } } Edge[i][j]=Edge[j][i]=ans; } } cout<<"The highest possible quality is 1/"<<prim()<<"."<<endl; } return 0;}
- zoj 2158 Truck History(最小生成树))
- POJ1789 Truck History(最小生成树)
- poj1789 Truck History (最小生成树)
- poj1789-Truck History(最小生成树)
- poj1789 - Truck History (最小生成树)
- POJ1789--Truck History(最小生成树)
- Truck History(最小生成树)
- POJ 一 1789 Truck History(最小生成树)
- POJ 1789 - Truck History(最小生成树)
- poj 1789 Truck History (最小生成树)
- POJ 1789 Truck History (最小生成树 Prim)
- POJ1789 Truck History(Prim最小生成树)
- A - Truck History(prim求最小生成树)
- POJ 1789:Truck History(prim&&最小生成树)
- poj 1789 Truck History (最小生成树)
- poj 1789 Truck History 最小生成树(prim)
- poj 1789 Truck History(最小生成树 Prim)
- POJ 1789 Truck History(最小生成树)
- SQL点滴26—常见T-SQL面试解析
- Add Binary
- Android service详解(一)
- hdu3468 splay
- mongoVUE 数据的json格式导入导出
- zoj 2158 Truck History(最小生成树))
- 为什么采用Always On SSL?
- 揭开Socket编程的面纱
- C/C++面试编程题
- 【刷题小记97】兄弟郊游问题
- SQL点滴30—SQL中常用的函数
- 在线音乐歌词实现方式
- 求一个字符串中连续出现最多的子串次数
- SQL点滴31—SQL语句中@@IDENTITY和@@ROWCOUNT区别