Truck History - poj 1789 (Prim 算法)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 20884 | Accepted: 8075 |
Description
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
Output
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x0fffffff;
char str[][];
int map[][];
int vis[];
int d[];
int n;
int Prim(){
for(int i=;i<n;i++){
d[i]=INF;
vis[i]=;
}
d[]=;
int sum=;
for(int i=;i<n;i++){
int mi=INF;
int p;
for(int j=;j<n;j++){
if(!vis[j]&&mi>d[j]){
mi=d[j];
p=j;
}
}
sum+=mi;
vis[p]=;
for(int j=;j<n;j++){
if(!vis[j]){
d[j]= min(d[j],map[p][j]);
}
}
}
return sum;
}
int main() {
cin>>n;
while(n){
for(int i=;i<n;i++){
for(int j=;j<;j++){
cin>>str[i][j];
}
}
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
int sum=;
for(int k=;k<;k++){
if(str[i][k]!=str[j][k]){
sum++;
}
}
map[i][j]=map[j][i]=sum;
}
}
int sum=Prim();
cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
cin>>n;
}
return ;
}
Truck History - poj 1789 (Prim 算法)的更多相关文章
- F - Truck History - poj 1789
有一个汽车公司有很多年的汽车制造历史,所以他们会有很多的车型,现在有一些历史学者来研究他们的历史,发现他们的汽车编号很有意思都是有7个小写字母组成的,而且这些小写字母具有一些特别的意义,比如说一个汽车 ...
- (最小生成树)Truck History --POJ -- 1789
链接: http://poj.org/problem?id=1789 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2213 ...
- Truck History POJ - 1789
题目链接:https://vjudge.net/problem/POJ-1789 思路: 题目意思就是说,给定一些长度为7的字符串,可以把字符串抽象为一个点, 每个点之间的距离就是他们本身字符串与其他 ...
- Truck History POJ - 1789 板子题
#include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> u ...
- POJ1789 Truck History 【最小生成树Prim】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18981 Accepted: 7321 De ...
- POJ-1789 Truck History---最小生成树Prim算法
题目链接: https://vjudge.net/problem/POJ-1789 题目大意: 用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一 ...
- Agri-Net - poj 1258 (Prim 算法)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44373 Accepted: 18127 Description F ...
- Highways - poj 2485 (Prim 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24383 Accepted: 11243 Description T ...
- POJ 1789 Prim
给定N个字符串,某个字符串转为另一个字符串的花费为他们每一位不相同的字符数. 求最小花费Q. Input 多组输入,以0结束. 保证N不超过2000. Output 每组输出"The hig ...
随机推荐
- bzoj 1879: [Sdoi2009]Bill的挑战
题目链接 bzoj 1879: [Sdoi2009]Bill的挑战 题解 n<=15,装压吧 对所有字符串进行装压 可以预处理一个数组can[i][j]表示所有的字符串中,有哪些可以在第i位匹配 ...
- Jekyll搭建个人博客-拓展版
关于Jekyll Jekyll 是一个简单的博客形态的静态站点生产机器.它有一个模版目录,其中包含原始文本格式的文档,通过 Markdown (或者 Textile) 以及 Liquid 转化成一个完 ...
- 【转载】GCC 预处理器选项
预处理器选项(Preprocessor Option) 下列选项针对C预处理器,预处理器用在正式编译以前,对C 源文件进行某种处理. 如果指定了`-E'选项, GCC只进行预处理工作.下面的某些选项必 ...
- 软路由OpenWrt教程收集(插件开发教程,opkg安装软件教程)
说明:现在几乎所有家庭级别的路由器都是基于OpenWrt进行衍生搭建的. https://openwrt.io/(极路由HiWifi创建的开源站点,极路由系统有这个衍生而来) http://www.o ...
- 获取SQLServer的最完整数据字典的SQL语句
原文:获取SQLServer的最完整数据字典的SQL语句 原创于2008年06月18日,2009年10月18日迁移至此. 获取SQLServer 的最完整数据字典的SQL 语句 其实网上已经流传了 ...
- Android开发中多进程共享数据
# 背景 最近在工作中遇到一个需求,需要在接收到推送的时候将推送获得的数据存起来,以供app启动时使用.我们会认为这不是So easy吗?只要把数据存到SharedPreferences中,然后让ap ...
- 【spring data jpa】spring data jpa的in查询
如下: List<Dealer> findDealersByTidAndUidIn(String tid,List<String> uidList); 在dao层里面直接写这个 ...
- 深度学习和web安全最新文章一览
先囤几篇文章: 1.https://www.cdxy.me/?p=773 2.https://segmentfault.com/a/1190000009052376 3.https://segment ...
- MIT算法导论笔记
详细MIT算法导论笔记 (网络链接) 第一讲:课程简介及算法分析 (Sheridan) 第二讲:渐近符号.递归及解法 (Sheridan) 第三讲:分治法(1)(Sheridan) 第四讲:快排及随 ...
- [ElasticSearch]Java API 之 词条查询(Term Level Query)
1. 词条查询(Term Query) 词条查询是ElasticSearch的一个简单查询.它仅匹配在给定字段中含有该词条的文档,而且是确切的.未经分析的词条.term 查询 会查找我们设定的准确值 ...