POJ 1789:Truck History:典型prim最小生成树(1)
来源:互联网 发布:屏幕碎裂特效 js 编辑:程序博客网 时间:2024/05/16 09:34
Truck History
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 17386 Accepted: 6672
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.
Source
CTU Open 2003
非常典型的prim算法,计算最小生成树,转化一下输入输出直接套用,没啥说的。
#include<stdio.h>#include<stdlib.h>#include<string.h>#define maxn 10000int n,d[2005],a[2005][2005],use[2005];char ming[2005][10];int find(int x,int y){int i,j;j=0;for(i=0;i<7;i++)if(ming[x][i]!=ming[y][i])j++;return j;}void init(){int i,j;for(i=0;i<n;i++){for(j=0;j<=i;j++)a[i][j]=0;d[i]=use[i]=0;}for(i=0;i<n;i++)scanf("%s",ming[i]);for(i=0;i<n;i++)for(j=0;j<i;j++)a[i][j]=a[j][i]=find(i,j);}void prim(){int i,j;int minc,mind;use[0]=1;d[0]=0;for(i=1;i<n;i++)d[i]=a[0][i];for(j=0;j<n-1;j++){minc=maxn;for(i=0;i<n;i++){if(!use[i]&&minc>d[i]){minc=d[i];mind=i;}}if(minc!=maxn){use[mind]=1;for(i=0;i<n;i++){if(!use[i]&&d[i]>a[mind][i])d[i]=a[mind][i];}}}}void print(){int i,j;int sum=0;for(i=1;i<n;i++)sum+=d[i];printf("The highest possible quality is 1/%d.\n",sum);}int main(){int i,j;while(scanf("%d",&n)!=EOF&&n){init();prim();print();}return 0;}
0 0
- POJ 1789:Truck History:典型prim最小生成树(1)
- POJ 1789 Truck History (最小生成树 Prim)
- POJ 1789:Truck History(prim&&最小生成树)
- poj 1789 Truck History 最小生成树(prim)
- poj 1789 Truck History(最小生成树 Prim)
- POJ 1789 Truck History(最小生成树-Prim)
- POJ 1789 Truck History (最小生成树---prim)
- POJ 1789 Truck History(最小生成树,prim算法)
- POJ-1789 Truck History(prim算法,最小生成树)
- poj 1789 Truck History prim最小生成树 基础!!!!
- POJ 1789 Truck History 图论 prim算法 最小生成树
- 【最小生成树之prim算法】POJ 1789---Truck History
- POJ 1789 Truck History 最小生成树 kruskal && Prim
- POJ 1789 Truck History 最小生成树 prim
- poj--1789 Truck History(最小生成树Prim算法)
- poj 1789--Truck History 最小生成树(prim)
- POJ 1789 Truck History (prim解决最小生成树问题)
- POJ 1789 Truck History prim的最小生成树题解
- WPF与WCF wwf
- [嵌入式新产品] 蓝牙遥控器
- 设计模式学习笔记——单例模式
- 比较详细Python正则表达式操作指南(re使用)
- Android4.0设置界面修改总结(三)
- POJ 1789:Truck History:典型prim最小生成树(1)
- 【C++】DISALLOW_COPY_AND_ASSIGN
- js方法内Ajax请求数据判断,验证无效(OnClientClick="return Method();"),还是直接执行后台代码
- unity + win8.1 apps 小游戏demo
- 个人简单通信录
- POJ 1328 Radar Installation 贪心
- Android Wear - App Structure for Android Wear(应用结构)
- uva 10719(数学)
- Python 解析XML文件