poj 1789 Truck History(Prim)(中等)
2015-07-22 14:52
369 查看
Truck History
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.
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
Sample Output
题意(摘自小優YoU的博客):
用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。
例如有如下4个编号:
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
显然的,第二,第三和第四编号分别从第一编号衍生出来的代价最小,因为第二,第三和第四编号分别与第一编号只有一个字母是不同的,相应的distance都是1,加起来是3。也就是最小代价为3。
问题可以转化为最小代价生成树的问题。因为每两个结点之间都有路径,所以是完全图。
此题的关键是将问题转化为最小生成树的问题。每一个编号为图的一个顶点,顶点与顶点间的编号差即为这条边的权值,题目所要的就是我们求出最小生成树来。这里我用prim算法来求最小生成树。
代码:
还是我的简便。。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20931 | Accepted: 8093 |
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.
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.
题意(摘自小優YoU的博客):
用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。
例如有如下4个编号:
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
显然的,第二,第三和第四编号分别从第一编号衍生出来的代价最小,因为第二,第三和第四编号分别与第一编号只有一个字母是不同的,相应的distance都是1,加起来是3。也就是最小代价为3。
问题可以转化为最小代价生成树的问题。因为每两个结点之间都有路径,所以是完全图。
此题的关键是将问题转化为最小生成树的问题。每一个编号为图的一个顶点,顶点与顶点间的编号差即为这条边的权值,题目所要的就是我们求出最小生成树来。这里我用prim算法来求最小生成树。
代码:
还是我的简便。。
//15628K 610MS #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int inf=10; const int maxn=2010; int n; char str[maxn][8]; int Map[maxn][maxn],dis[maxn],vis[maxn]; int weight(int i,int j) { int w=0; for(int k=0;k<7;k++) if(str[i][k]!=str[j][k]) w++; return w; } void prim() { int Min,pos; for(int i=1;i<=n;i++) { vis[i]=0; dis[i]=Map[1][i]; } for(int i=1;i<=n;i++) { Min=inf; for(int j=1;j<=n;j++) { if(!vis[j] && dis[j]<Min) { Min=dis[j]; pos=j; } } vis[pos]=1; for(int j=1;j<=n;j++) { if(!vis[j] && dis[j]>Map[pos][j]) dis[j]=Map[pos][j]; } } int sum=0; for(int i=1;i<=n;i++) sum+=dis[i]; printf("The highest possible quality is 1/%d.\n",sum); } int main() { while(scanf("%d",&n)&&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++) Map[i][j]=Map[j][i]=weight(i,j); prim(); } return 0; }
相关文章推荐
- js实现分享到人人网微信和qq等等
- JStorm学习笔记
- Android手机怎么获取打印机的IP地址呢
- Netty实现服务端客户端长连接通讯及心跳检测
- POJ 1849 Two (树形dp 树的直径 两种方法)
- cron服务 ubuntu
- 添加和删除索引以及如何给中间表添加两个主键
- PHP使用array_merge重新排列数组下标的方法
- Android中Java和JS双向调用
- 要怎么正确判断一个企业呢
- LeetCode—数组(1)
- 环形RNA预测注意事项
- Spring MVC 4常用的那些注解 .
- windows编程--网络编程学习--winsock编程(2)
- html5 绘制椭圆
- Android访问网络 使用HttpURLConnection还是HttpClient?
- 学习笔记:js、css、html判断浏览器的各种版本
- maven2 环境搭建(1)
- 微信企业号开发:消息类型与区别
- C宏定义#和##的用法