Truck History-(prime-算法)
来源:互联网 发布:在淘宝上买了二手钢琴 编辑:程序博客网 时间:2024/06/01 10:11
Truck History
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 67 Accepted Submission(s) : 23
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.
最小生成树问题,一个字符串可以衍生出另一个字符串,两个字符串间不同字母的个数d(t0,td)要累加,求一个衍生顺序使得最后累加和最小,每次都取一个t0衍生出的产生最小d的字符串td做下一次的t0,注意题目中初始t0是第一串字符,就是Prime算法
代码:
#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;#define inf 1000000int a[2001][2001];char s[2001][8];int dis[2001];bool ok[2001];inline int different(int a,int b){ int num=0; for(int i=0;i<7;i++) { if(s[a][i]!=s[b][i]) num++; } return num;}int main(){ int n,i,j,k,cnt; while(scanf("%d",&n)&&n) { for(i=1;i<=n;i++) { cin>>s[i]; for(j=1;j<=n;j++) a[i][j]=0; } for(i=1;i<=n;i++) //将数组两两间的差值放入矩阵 { for(j=1;j<=n;j++) { if(i==j) {continue;} if(a[i][j]==0) a[j][i]=a[i][j]=different(i,j); } } cnt=inf; memset(dis,0x7f,sizeof(dis)); //dis[i]代表未放入树中的i字符串如树中字符串的最小差值 dis[1]=0; memset(ok,true,sizeof(ok)); //ok数组代表使用情况,初始化后所有点都未在树中 int total=0; for(j=1;j<=n;j++) { int t=0; for(k=1;k<=n;k++) //找一个与树中字符串差值最小的字符串t if(ok[k]&&(dis[k]<dis[t])) t=k; ok[t]=false; //放入树中 total+=dis[t]; for(k=1;k<=n;k++) //修改与t相连字符串的差值 if(ok[k]&&a[t][k]<dis[k]) dis[k]=a[t][k]; } if(total<cnt) cnt=total; printf("The highest possible quality is 1/%d.\n",cnt); } return 0;}
阅读全文
0 0
- Truck History-(prime-算法)
- Truck History (Prim算法)
- POJ 1789 Truck History(最小生成树--prime)
- poj 1789 Truck History(prim算法)
- poj 1789 Truck History(kruskal算法)
- poj 1789 Truck History (普里姆算法)
- Truck History 最小生成树 prime
- Truck History(最小生成树prime)
- POJ_1789(Truck History )(最小生成树(普里姆prim算法))
- POJ 1789 Truck History(最小生成树,prim算法)
- POJ-1789 Truck History(prim算法,最小生成树)
- poj_1789 Truck History(prim)
- poj 1789Truck History(最小生成树 prime)
- poj1789 Truck History(Prim算法实现)
- poj 1789 Truck History(kruskal算法实现)
- Truck History(最小生成树_prim算法)
- Truck History POJ 1789【Kruscal算法 || Prim】
- POJ-1789 Truck History(Prim算法)
- 树莓派新手上路
- JQ测验
- 使用ImageLoader从网上获取图片 及添加数据库,listview展示 Gson解析
- 使用windows远程桌面连接ubuntu
- C++写ATM机
- Truck History-(prime-算法)
- 安全工具介绍
- Spark Storage之MemoryStore
- 技术团队新官上任之基层篇
- dom4j和sax解析XML文档以及简单工厂模式介绍
- CSS布局
- 两个链表的第一个公共结点
- Vue环境搭建
- Java对象与字节工具类简单实现