POJ 1789 Truck History
来源:互联网 发布:没网能看电视的软件 编辑:程序博客网 时间:2024/06/12 00:04
Truck History
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 28957 Accepted: 11305
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.
千万不要被这个又臭又长的题目还有这个蜜汁公式吓到,这就是一道水题......
题目大意:用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。
用prim算法
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int inf=0x3f3f3f3f;char str[2010][10];int map[2010][2010];int dis[2010];int book[2010];int n;void prim(){memset(book,0,sizeof(book));for(int i=0;i<n;i++) dis[i]=map[0][i];book[0]=1;int ans=0;for(int i=0;i<n;i++){int min=inf,flag=0;for(int j=0;j<n;j++){if(!book[j]&&dis[j]<min){min=dis[j];flag=j;}}if(min==inf) break;book[flag]=1;ans+=min;for(int j=0;j<n;j++){if(!book[j]&&dis[j]>map[flag][j]){dis[j]=map[flag][j];}}}printf("The highest possible quality is 1/%d.\n",ans);}int main(){while(~scanf("%d",&n)){if(!n) break;for(int i=0;i<n;i++) scanf("%s",str[i]);for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){int flag=0;for(int k=0;k<7;k++){if(str[i][k]!=str[j][k]){flag++;}}map[i][j]=map[j][i]=flag;}}prim();}}
阅读全文
0 0
- POJ 1789 Truck History
- POJ 1789 Truck History
- POJ 1789 Truck History
- poj 1789 Truck History
- poj 1789 Truck History
- Poj 1789 Truck History
- poj 1789 Truck History
- poj 1789 Truck History
- poj 1789 Truck History
- poj 1789 Truck History
- POJ-1789-Truck History
- poj-1789-Truck History
- POJ 1789 Truck History
- poj 1789 Truck History
- POJ 1789 Truck History
- POJ 1789 Truck History
- POJ 1789 Truck History
- POJ 1789 Truck History
- 电话本字母侧边栏
- InputMethodManager引起的内存泄漏问题分析与解决
- 图数据库选型和调研
- js高级教程阅读笔记 第五章-引用类型(5.5)
- Java多线程之线程的挂起与恢复(Suspend/Resume)
- POJ 1789 Truck History
- leetcode--Unique Binary Search Trees
- php7.0 出现 curl_setopt(): Disabling safe uploads is no longer supported in 报错!
- 全面了解Objective-C:Copy
- ssm项目---人事管理系统:员工与部门、职位实现一对一
- MTK Android Driver:PMIC
- iOS开发
- WebSocket协议
- JS中数组的常用方法(包含es6扩展)