POJ 1789 Truck History
来源:互联网 发布:Hec-ras 数据输入方法 编辑:程序博客网 时间:2024/05/18 22:45
Truck History
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14877 Accepted: 5689
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
题意:
用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数
一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance
现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小
代码:
/*Prim*/#include<iostream>#include<cstring>using namespace std;#define INF 0x3f3f3f3f#define MAX 2002int map[MAX][MAX];int dist[MAX];bool vist[2010];char str[2010][10];int n;int diff(int x,int y){int i,w=0;for(i=0;i<7;i++)if(str[x][i]!=str[y][i])w++;return w;}int Prim(){int i,j,k,min,flag=0,ans=0;for(i=1;i<=n;i++)dist[i]=map[1][i];vist[1]=true;for(i=1;i<n;i++){min=INF;for(j=2;j<=n;j++)if(min>dist[j] && !vist[j]){min=dist[j];flag=j; }vist[flag]=true;ans+=min;for(k=1;k<=n;k++)if(dist[k]>map[flag][k] && !vist[k])dist[k]=map[flag][k];}return ans;}int main(){int i,j;while(cin>>n!=NULL && n){memset(vist,false,sizeof(vist));memset(map,INF,sizeof(map));memset(dist,INF,sizeof(dist));for(i=1;i<=n;i++){cin>>str[i];for(j=1;j<=i;j++)map[i][j]=map[j][i]=diff(i,j);}cout<<"The highest possible quality is 1/"<<Prim()<<"."<<endl;}return 0;}
思路:
求最小的“衍生”方案即为求最小生成树
将每个string的不同字母数存储构成一个map
- 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
- JAVA中的文件删除
- 杨氏矩阵查找元素位置Java实现
- thrift之TTransport层的缓存传输类TBufferedTransport和缓冲基类TBufferBase
- form程序利用控制台输出
- wince复制文件时无“取消”按钮问题总结
- POJ 1789 Truck History
- 180天编写180个网站:他是这么学习编程的,如今已到115天
- Xp/Win7下的VC6的调试、多开工程实例等问题的完全兼容解决方案
- (Problem 33)Digit canceling fractions
- 根据正则表达式生成随机字符串
- 【Java学习笔记】基础知识学习15【文本输入】
- Server 2008 R2 如何支持串口UPS电源管理
- Codeforces Round #193 (Div. 2)
- C语言的变量的内存分配