poj1789 最小生成树
题目连接: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
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define MAX 2005 #define INF 14005 using namespace std;int G[MAX][MAX]; int d[MAX]; bool vis[MAX]={0}; char a[MAX][MAX]; int n;int prim(int s) {vis[s]=1;for(int i=0;i<n;i++)d[i]=G[s][i];int now,m;int res=0;for(int i=1;i<n;i++){m=INF;for(int j=0;j<n;j++){if(!vis[j]&&d[j]<m){now=j;m=d[j];}}vis[now]=1;res+=m;for(int j=0;j<n;j++){if(!vis[j]&&d[j]>G[now][j])d[j]=G[now][j];}}return res; }void init() {memset(vis,0,sizeof(vis));for(int i=0;i<n;i++)for(int j=0;j<n;j++)G[i][j]=INF; }int getdis(char * a,char * b) {int res=0;for(int i=0;i<7;i++){if(a[i]!=b[i])res++;}return res; }int main() {while(scanf("%d",&n)!=EOF){if(n==0)return 0;init();for(int i=0;i<n;i++)scanf("%s",a[i]);for(int i=0;i<n;i++)for(int j=0;j<n;j++)G[i][j]=getdis(a[i],a[j]);printf("The highest possible quality is 1/%d.\n",prim(0));} }
转载于:https://www.cnblogs.com/cryingrain/p/8728740.html
poj1789 最小生成树相关推荐
- POJ-1789 Truck History 最小生成树
计算整个汽车演化过程中所要改变的最少的字符数.改变的字符数的计算方式为1-7位不同位的个数. 代码如下: #include <cstdlib> #include <cstring&g ...
- poj1789 Truck History(最小生成树)
2018-3-24 简单的最小生成树问题. 题目大意是: 给你n个字符串,他们的distance就是串中不同字符的个数,要求算出所有串的distance's 最小 sum : #include< ...
- POJ - 1789 Truck History (最小生成树)
https://vjudge.net/problem/POJ-1789 题意 n个车牌,长度固定为7,车牌与车牌间的距离为不同字母个数.问所有车牌形成一棵树的最小边权和是多少. 分析 最小生成树模板 ...
- 数据结构与算法(7-3)最小生成树(普里姆(Prim)算法和克鲁斯卡尔(Kruskal)算法)
目录 一.最小生成树简介 二.普里姆算法(Prim) 1.原理 2.存储 2-1.图顶点和权: 2-3. 最小生成树: 3.Prim()函数 3-1.新顶点入树 3-2.保留最小权 3-3. 找到最小 ...
- [kuangbin带你飞]专题六 最小生成树 L - 还是畅通工程 (简单最小生成树)
L - 还是畅通工程 题目链接:https://vjudge.net/contest/66965#problem/L 题目: 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府&qu ...
- 图的算法专题——最小生成树
概要: Prim算法 Kruskal算法 1.Prim算法 算法流程: (1)对图G(V,E)设置集合S来存放已被并入的顶点,然后执行n次(2)(3) (2)每次从未并入顶点集合中选择与集合S最近的一 ...
- 【BZOJ1016】【Luogu P4208】 [JSOI2008]最小生成树计数 最小生成树,矩阵树定理
蛮不错的一道题,遗憾就遗憾在数据范围会导致暴力轻松跑过. 最小生成树的两个性质: 不同的最小生成树,相同权值使用的边数一定相同. 不同的最小生成树,将其都去掉同一个权值的所有边,其连通性一致. 这样我 ...
- Educational Codeforces Round 9 F. Magic Matrix 最小生成树
F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...
- [vijos1234]口袋的天空最小生成树
题目链接:https://vijos.org/p/1234 白天刚刚写完prim的算法,晚上就心血来潮的打了一道最小生成树的题 虽然有题解说可以用prim做,但是这道题明显是加最小的边,感觉krusk ...
最新文章
- 万航单位换算器 V1.0 绿色版
- “ yield”关键字有什么作用?
- eclipse的tomcat插件使用下载(转)
- redis 亿级查询速度_亿级流量系统架构之如何保证百亿流量下的数据一致性(上)...
- python中的常量_Python中的变量和常量
- 信息学奥赛一本通 1146:判断字符串是否为回文 | OpenJudge NOI 1.7 33:判断字符串是否为回文
- Java的反射(二)
- 测试你的杀毒软件实时监控能力!
- android listview 预加载动画,Android - 使用预加载的数据库填充ListView
- Thymeleaf 用法
- CSS 魔法系列:纯 CSS 绘制各种图形《系列六》
- 2014年终总结回顾与2015年工作总结
- MATLAB直方图图像去雾算法实现
- 怎样在 Linux 系统中恢复已删除文件
- 行业洞察系列之《事件管理的 5 个阶段及其改进建议》
- 佑道医生集团获风和资本数千万A轮投资,6个月实现盈亏平衡
- 三表左连接sql语句例子
- ipad如何分屏_基于ipad的生态型无纸化学习说明书
- 名著赏读 | 雅思贝尔斯的《什么是教育》各章内容概要
- 2023 华为 Datacom-HCIE 真题题库 02/12--含解析
热门文章
- java 标准_Java标准注解
- python串口数据分包_python TCP Socket的粘包和分包的处理详解
- 软件测试:面试屡屡碰壁,只因你身上少了这几个特征!
- 怎么让图片铺满手机屏幕_手机屏幕密码忘了怎么解锁
- php自动跳转函数,迅睿CMS 重写控制网站自动跳转函数
- pythonmatplot可视化_python:matplotlib基础数据可视化,pythonmatplotlib
- python怎么复数乘方开方_孩子数学不好怎么办?怎样让孩子学好数学的方法
- matlab虚拟现实之V-Realm Builder2建模注意事项
- activimq java集成_Java消息队列-Spring整合ActiveMq
- oracle多少钱一套_消防水炮多少钱一套?您真的只需要一套吗