poj1789 Truck History
来源:互联网 发布:温州网络学堂挂机 编辑:程序博客网 时间:2024/06/05 00:32
Truck History
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 29606 Accepted: 11581
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的字符串,字符串和另一个字符串的“距离”定义为同一位置上不同字符的个数。然后这些字符串是互相分化而来的,每一个字符串会分化出其他的字符串。问你怎么让分化的总距离尽可能短。
题解:直接最小生成树
#include<stdio.h>using namespace std;const int MAX = 2000 + 10;const int INF = 0x3f3f3f3f;int G[MAX][MAX];int dist[MAX];int addvnew[MAX];int n;char str[MAX][8];int prim(int s){int sum = 0;for(int i = 1; i <= n; ++i){dist[i] = G[s][i];addvnew[i] = 0;}addvnew[s] = 1;for(int i = 1;i <= n ; ++i){if(i != s){int min = INF;int v = -1;for(int j = 1; j <= n; ++j){if(!addvnew[j] && dist[j] < min){min = dist[j];v = j;}}if(v != -1){addvnew[v] = 1;sum += dist[v];for(int j = 1;j <= n; ++j){if(!addvnew[j] && G[v][j] < dist[j]){dist[j] = G[v][j];}}} }}//cout << "minmum sum:" << sum << "\n";return sum;} int cmp(int i, int j){int cnt = 0; for(int k = 0;k < 7; ++k)if(str[i][k] != str[j][k])++cnt;return cnt;}int main(){while(scanf("%d",&n) == 1 && n){for(int i = 1;i <= n; ++i){scanf("%s",str[i]);}for(int i = 1;i <= n; ++i){for(int j = 1;j <= n; ++j){G[i][j] = G[j][i] = cmp(i,j);}}printf("The highest possible quality is 1/%d.\n",prim(1));}}
一开始用邻接表居然MLE了,我也是醉了。改成邻接矩阵反而AC。
阅读全文
0 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
- Hibernate的索引
- 南阳oj 123 树状数组入门 区间更新 单点求值
- 我的OPENCV学习笔记-图像混合
- 基于CUDA在GPU上实现膨胀、腐蚀加速
- 关于201709CSP的总结
- poj1789 Truck History
- ElastAlert对ELK日志进行邮箱报警
- hibernate 初步
- Uva 1347 旅行
- Vue项目 build构建
- 面试题
- 《零基础入门学习python》学习过程(四)
- 数据结构-线性表-顺序表
- Bootstrap使用总结