POJ1789-Truck History
来源:互联网 发布:淘宝卖家售假 编辑:程序博客网 时间:2024/06/06 00:09
Truck History
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.
题解: 这个题prime,和Kruskal算法都能过。
prime算法
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char mapp[2010][10];int vis[2010];int f[2010];int n;const int inf=0x3f3f3f3f;int fun(int i,int j){ int t=0; for(int k=0;k<7;k++) { if(mapp[i][k]!=mapp[j][k]) t++; } return t;}int prime(){ int sum=0, k,min; for(int i=0;i<n;i++) f[i]=fun(0,i); f[0]=0; vis[0]=1; for(int i=0;i<n-1;i++) { min=0x3f3f3f3f; for(int j=0;j<n;j++) { if(!vis[j]&&f[j]<min) { min=f[j]; k=j; } } sum+=min; vis[k]=1; for(int j=0;j<n;j++) { if(!vis[j]&&f[j]>fun(k,j)) f[j]=fun(k,j); } } return sum; }int main(){ while(scanf("%d",&n)!=EOF&&n) { for(int i=0;i<n;i++) scanf("%s",mapp[i]); memset(vis,0,sizeof(vis)); printf("The highest possible quality is 1/%d.\n",prime()); }}
Kruskal算法
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<algorithm>#define MAX 2000*2000using namespace std; int pre[MAX]; struct node1{ char str[100];}a[2001];//输入字符串struct node { int u; int v; int distance; //存储距离}p[MAX]; bool cmp(node a, node b) { return a.distance < b.distance; } int find(int x){ if(pre[x]==x) return x; return pre[x]=find(pre[x]);}void init(int n){ for(int i=0;i<n;i++) { pre[i]=i; }}int main() { int n; // getchar(); while(scanf("%d",&n)!=EOF&&n!=0) { getchar();//消回车 init(n); for(int i=0;i<n;i++) { gets(a[i].str); } int k=0; for(int i=0;i<n-1;i++) { for(int j=i;j<n;j++) { int num=0; p[k].u=i; p[k].v=j;//记录村庄i j for(int s=0;s<7;s++) { if(a[i].str[s]!=a[j].str[s]) { num++; }//如果对应位置字符不同,用num++累加距离 } p[k++].distance=num;//统计村庄IJ间的距离 } } sort(p,p+k,cmp); int ans=0; for(int i = 0; i < k; i++) { int x = find(p[i].u); int y = find(p[i].v); if(x != y) { ans = ans + p[i].distance; // printf("%d !!!\n",p[i].distance); pre[y] = x; } } printf("The highest possible quality is 1/%d.\n",ans); } return 0; }
- POJ1789 Truck History
- Truck History(POJ1789)
- poj1789 - Truck History
- Truck History poj1789
- poj1789 Truck History
- poj1789--Truck History
- poj1789 Truck History
- poj1789 Truck History(Prim)
- POJ1789 Truck History【Prim】
- POJ1789--Truck History
- poj1789 Truck History
- POJ1789 Truck History
- POJ1789 Truck History
- POJ1789-Truck History .
- POJ1789 Truck History
- POJ1789 Truck History
- POJ1789 Truck History
- POJ1789 Truck History
- struts2重点、精华、用法总结(一)
- (6)Array对象
- C语言中volatile关键字的作用
- 关注民生民情——华北水利水电大学“情艺”国情社情调查
- Android Studio用小米手机调试安装不上应用error while installing apks
- POJ1789-Truck History
- Linux下U盘无法挂载以及文件夹出错
- poj-1287-Networking-最小生成树
- Java学习之旅--线程的创建方法
- java窗体Swing效果体验(并没有什么卵用)
- sync同步指令
- HDU 1305 Immediate Decodability (字典树)
- Android Studio 与git 添加忽略文件
- 认识BeanPostProcessor接口