Truck History(最小生成树)
来源:互联网 发布:建筑图纸设计软件 编辑:程序博客网 时间:2024/06/01 14:38
Problem 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.
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
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
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample Output
The highest possible quality is 1/3
题意:
有n个字符串,每一个字符串都是7个字母,字符串之间的距离是 他们字母不同的个数,然后求串通他们的最小的距离。
看出是最小生成树就好了,直接套用模板。
#if 0#include<iostream>#include<cstring>using namespace std;int mapp[3000][3000];int minn[3000],n;bool u[3000];void prime(){minn[1]=0; for(int i=1; i<=n; i++) // {int k=0;for(int j=1; j<=n; j++) //找出与白点 连接权值最小的蓝点 k {if(!u[j]&&minn[k]>minn[j]){k=j;}}u[k]=1; //k 蓝点 for(int j=1; j<=n; j++){if(!u[j]&&mapp[k][j]<minn[j])minn[j]=mapp[k][j];} } }int main(){while(cin>>n&&n){string s[3000];memset(mapp,0,sizeof(mapp));memset(minn,0x7f,sizeof(minn));memset(u,0,sizeof(u));for(int i=1; i<=n; i++){cin>>s[i];}for(int i=1; i<=n; i++){for(int j=1; j<=n; j++){int x=0;for(int k=0; k<7; k++){if(s[i][k]!=s[j][k])x++;}mapp[i][j]=x;}}int sum=0;prime();for(int i=1; i<=n; i++){sum+=minn[i];}cout<<"The highest possible quality is 1/"<<sum<<"."<<endl; } }#endif
阅读全文
0 0
- POJ1789 Truck History(最小生成树)
- poj1789 Truck History (最小生成树)
- poj1789-Truck History(最小生成树)
- poj1789 - Truck History (最小生成树)
- POJ1789--Truck History(最小生成树)
- Truck History(最小生成树)
- zoj 2158 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(最小生成树)
- 匿名对象和匿名类
- [Codeforces 590E] Birthday
- 凸包模板
- oracle单引号和双引号的区别
- 接口与多态
- Truck History(最小生成树)
- 自己初步了解python数据爬虫
- Hdu 4003 Find Metal Mineral 树型背包DP
- 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数), 凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
- 找规律 permutation
- UVA 442 栈解析表达式
- oracle(四)
- Java反射
- SQL ORDER BY 子句