POJ1789:Truck History(Prim算法)
http://poj.org/problem?id=1789
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.
此题难点在于读题,明白题意后就是一个很简单的最小生成树
明白一点就行:将每一个卡车类型代码(truck type codes)作为一个结点,任意两个 卡车类型代码中 相同位置即字符数组a[i],a[i+1] 上 a[i]和a[i+1]为不同字符的位置的个数 做为
这两个结点之间的路径的权值。
读题啊,硬伤啊!
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #define INF 0x3f3f3f3f
- using namespace std;
- char a[][];
- int map[][];
- int n,dis[],v[];
- void prim()
- {
- int min,sum=,k;
- for(int i=; i<=n; i++)
- {
- v[i]=;
- dis[i]=INF;
- }
- for(int i=; i<=n; i++)
- dis[i]=map[][i];
- v[]=;
- for(int j=; j<n; j++)
- {
- min=INF;
- for(int i=; i<=n; i++)
- {
- if(v[i]==&&dis[i]<min)
- {
- k=i;
- min=dis[i];
- }
- }
- sum+=min;
- v[k]=;
- for(int i=; i<=n; i++)
- {
- if(v[i]==&&map[k][i]<dis[i])
- {
- dis[i]=map[k][i];
- }
- }
- }
- cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
- }
- int main()
- {
- int count;
- while(scanf("%d",&n)!=EOF&&n!=)
- {
- for(int i=; i<=n; i++)
- scanf("%*c%s",a[i]);
- for(int i=; i<=n; i++)
- {
- for(int j=i+; j<=n; j++)
- {
- count=;
- for(int k=; k<; k++)
- if(a[i][k]!=a[j][k])
- {
- count++;
- }
- map[i][j]=count;
- map[j][i]=count;
- }
- }
- prim();
- }
- return ;
- }
POJ1789:Truck History(Prim算法)的更多相关文章
- POJ1789 Truck History(prim)
题目链接. 分析: 最大的敌人果然不是别人,就是她(英语). 每种代表车型的串,他们的distance就是串中不同字符的个数,要求算出所有串的distance's 最小 sum. AC代码如下: #i ...
- POJ1789 Truck History 【最小生成树Prim】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18981 Accepted: 7321 De ...
- Truck History(prim & mst)
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19772 Accepted: 7633 De ...
- poj1789 Truck History
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...
- poj1789 Truck History最小生成树
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...
- POJ1789 Truck History 2017-04-13 12:02 33人阅读 评论(0) 收藏
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27335 Accepted: 10634 D ...
- Truck History(prim)
http://poj.org/problem?id=1789 读不懂题再简单也不会做,英语是硬伤到哪都是真理,sad++. 此题就是一个最小生成树,两点之间的权值是毎两串之间的不同字母数. #incl ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- POJ-1789 Truck History---最小生成树Prim算法
题目链接: https://vjudge.net/problem/POJ-1789 题目大意: 用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一 ...
随机推荐
- WP8.1学习系列(第二十六章)——控件模板
在本文中 自定义控件模板示例 指定控件的可视结构. 指定控件的可视行为 使用工具轻松处理主题 控件和辅助功能 了解有关控件默认模板的详细信息 控件模板中的主题资源 相关主题 在 XAML 框架中,如果 ...
- canvas - drawImage()方法绘制图片不显示的问题
canvas有个很强大的api是drawImage()(w3c): 他的主要功能就是绘制图片.视频,甚至其他画布等. 问题: 慕名赶来,却一脚踩空,低头一看,地上一个大坑. 事情是这样的,在我看完 ...
- * 和-> 优先级
(Apple *)pf->peel(); 则报错说 ct.cpp: In function ‘int main()’:ct.cpp:48: 错误:void 值未如预期地被忽略 ...
- vue2.0在table中实现全选和反选
其实在去年小颖已经写过一篇:Vue.js实现checkbox的全选和反选 小颖今天在跟着慕课网学习vue的过程中,顺便试试如何在table中实现全选和反选,页面的css样式是直接参考慕课网的样式写的, ...
- sencha touch NavigationView
NavigationView 是官方根据Container控件扩展而来的,由一个导航栏和一个card组成,具备导航和返回时自动销毁当前界面的功能,非常适合新手使用. 其中导航栏的代码如下: Ext.d ...
- Openstack Nova network
对于安装设置来说,Openstack就剩下网络这个地方比较复杂. 现在比较喜欢看图 整理了一下网络的资料 1:Nova 网络HA http://unchainyourbrain.com/opensta ...
- vue生成路由实例, 使用单个vue文件模板生成路由
一.vue-loader与vue-router配合 $ cnpm install vue-router --save 二.生成vue-webpack模板 $ vue init webpack-simp ...
- Maven:版本管理 【SNAPSHOT】【Release】【maven-release-plugin】【nexus】
什么是版本管理 首先,这里说的版本管理(version management)不是指版本控制(version control),但是本文假设你拥有基本的版本控制的知识,了解subversion的基本用 ...
- linux中守护进程启停工具start-stop-daemon
1.功能作用 启动和停止系统守护程序 2.位置 /sbin/start-stop-daemon 3.主要参数 Commands: -S|--start -- <argument> ... ...
- 盘古分词修改支持mono和lucene.net3.03
盘古分词平台兼容性 在使用Lucece.net,需要一个中文的分词组件,比较好的是盘古分词,但是我希望能够在mono的环境下运行,就使用moma检查了一下盘古分词 Assembly Version M ...