百练 1789: Truck History

【代码超详解】POJ 1789 Truck History(Kruskal 算法) 一、题目描述 二、算法分析说明与代码编写指导 三、AC 代码 #include<cstdio> #include<algorithm> #pragma warning(disable:4996) using namespace std; template<size_t n> class union_find { private: unsigned root... 阅读详情

百练 1789: Truck History

原题OJ链接:http://bailian.openjudge.cn/practice/1789/

描述

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
∑(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.

输入

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.

输出

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.

样例输入

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

样例输出

The highest possible quality is 1/3.

解题思路

最小生成树,把每一个truck type code 看成一个节点(aaaaaaa为节点0,baaaaaa为节点1,abaaaaa为节点2,aabaaaa为节点3)。每两个truck type code之间的距离即为相同位置处字母不同的个数,aaaaaaa和baaaaaa之间的距离为1,baaaaaa和abaaaaa之间的距离为2,第一位和第二位不一样。题目中要求距离之和的最小值,转化为求解一棵最小生成树。

源代码

#include<iostream>
#include<cstring> 
#include<algorithm>
#define INF 999999999
#define MAX_N 2100
using namespace std;

int cost[MAX_N][MAX_N];
int mincost[MAX_N];
bool used[MAX_N];
int V;//节点数
string types[MAX_N];//truck type code


int prim(){
    for(int i=0;i<V;i++){
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0]=0;
    int res=0;
    while(true){
        int v=-1;
        for(int u=0;u<V;u++){
            if(!used[u] && (v==-1 || mincost[u]<mincost[v]))
                v=u;
        }
        if(v==-1) break;
        used[v]=true;
        res += mincost[v];

        for(int u=0;u<V;u++){
            mincost[u]=min(mincost[u],cost[v][u]);
        }      
    }
    return res;
}


int main(){
    while(cin>>V && V!=0){
        for(int i=0;i<V;i++){
            for(int j=0;j<V;j++){
                cost[i][j]=INF;
            }
        }
        for(int i=0;i<V;i++){
            cin>>types[i];
        }
        for(int i=0;i<V;i++){
            for(int j=i;j<V;j++){
                int count=0;
                for(int n=0;n<7;n++){
                    if(types[i][n]!=types[j][n]){
                        count++;
                    }
                }
                cost[i][j]=count;
                cost[j][i]=count;
            }
        }
        int Q=prim();
        cout<<"The highest possible quality is 1/"<<Q<<"."<<endl;
    }
    return 0;
}
littlefs系列:wear leveling wear leveling的功能是通过block alloctor实现的。littlefs通过两个方法实现磨损均衡: 1) 检测和恢复bad blocks 2)通过dynamic blocks均衡磨损 检测和恢复bad blocks和block alloctor的关系并不紧密,主要依靠文件系统本身来检测坏块并标记。在littlefs中,检测坏块主要是通过写后回读并比较的方式实现的,如果数据不一致则说明是一个坏块。一旦检测到了坏块就要进行恢复。 如果是写入错误,因为数据才RAM中还有备份,因此需. 阅读详情

相关推荐

单相PWM整流器搭建训练——(一)拓扑原理介绍

单相整流器是一种将交流电转换为直流电的设备,主要由变压器、整流元件和滤波电路组成。变压器将输入的交流电压转换为适合整流的较低电压,整流元件(如二极管)利用其单向导电性将交流电转换为脉动直流电,最后滤波电路去除脉动直流电中的纹波,输出较为平滑的直流电,从而实现交流到直流的转换,广泛应用于各类电源设备中。

DikouNass的博客 1587

HRBUST 1789 通信道路(思维)

通信道路 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 36(25 users) Total Accepted: 20(19 users) Rating:  Special Judge: No Description      某

xiaotuji的博客 615

自动化测试软件设计:C# + VISA 库实现多接口(GPIB/USB)测试仪器控制

本文介绍了如何利用C#和VISA库开发支持多接口的自动化测试软件。VISA库作为标准化API,能够统一控制GPIB、USB等不同接口的测试设备。文章详细阐述了软件设计目标,包括多接口支持、统一控制、自动化脚本执行等功能。重点讲解了C#中VISA库的安装配置,并提供了设备控制类的核心代码实现,涵盖设备连接、命令发送、响应读取等关键操作。通过具体示例展示了GPIB和USB设备的连接方式,并扩展了自动化测试脚本功能。该方案具有高扩展性和维护性,适用于电子设备测试、生产线检测等多种场景,为自动化测试系统开发提供了实

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1712

"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 C.Collection Game【递推】

Collection Game Time Limit: 1000 MS Memory Limit: 128000 K Total Submit: 41(21 users) Total Accepted: 22(20 users) Rating:  Special Judge: No Description

mengxiang000000 969

多视图学习 (Multi-View Learning)

多视角/多视图学习(Multi-view learning)是陶大成提出的一个研究方向。我们都知道,在机器学习中样本可以用不同的特征(如图片可以用LBP、SIFT等特征)来表示,或者从不同的角度进行观察(如从前后左右观察一个对象),甚至是采用不同的传感器来观测(如RGB摄像头,Depth摄像头),这些不同的观测手段、角度或特征在多视角学习中称为“视角”。多视角学习通过对不同的视角进行统一分析和研究,希望能够得到更好的分类或聚类等等的效果。

CarpeDiem 3万+

SEED-VIG数据集实战:5步搞定驾驶员疲劳检测模型(附Python代码)

本文提供了一份基于SEED-VIG数据集的驾驶员疲劳检测模型实战指南。通过五个核心步骤——数据准备、特征提取、模型训练、优化评估及部署思路,详细讲解了如何利用脑电信号构建分类器,并附有完整的Python代码,帮助开发者快速上手这一智能座舱关键技术。

w1x2y3的博客 263

POJ1789-Truck History

全解题报告索引目录 -> 【北大ACM – POJ试题分类】 转载请注明出处:http://exp-blog.com ------------------------------------------------------------------------- 题意大概是这样的:用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字...

ζёСяêτ - 小優YoU 9374

【正点原子I.MX6U-MINI移植篇】Ubuntu-base根文件系统移植构建过程详解(四)

能不能在ARM板上运行Ubuntu呢?答案肯定是可以的,Ubuntu是Linux系统的一种,可以简单的将Ubuntu理解为一个根文件系统,和我们用busybox、buildroot制作的根文件系统一样。因此移植Ubuntu也就是将Ubuntu根文件系统移植到我们的开发板上。

果果小师弟的博客 3767

[POJ](1789)Truck History ---最小生成树(图)

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29148   Accepted: 11392 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some truc...

WangMeow --- ᶘ ᵒᴥᵒᶅ ฅ^•ﻌ•^ฅ 340

keil软件调试(Debug)仿真教程(软件调试和硬件调试的区别)及常用调试按键详解

单片机的调试分为两种,一种是使用软件模拟调试,第二种是硬件调试,两种调试方式各有不同,软件模拟调试有误差,而硬件调试,借用仿真器调试是嵌入式开发中常用方法。

weixin_51624736的博客 3万+

【Unity实战】实现最完美最简单的UI适配方案,背景压缩或者拉伸变形解决方案,手机刘海屏适配,适配不同分辨率,不同尺寸,不同设备。

这期来讲一个简单的UI设计方案,很多同学可能搞不懂锚点、轴心这些概念,导致做好的UI在别人的设备上,乱跑或者是重叠,或者是参加游戏老发时间比较紧来不及做适配,都可以来试试这个方法,通过填充边框来保持游戏画面的原始比例,所以它的缺点也很明显,在一些移动设备上会浪费比较大的显示面积。 unity不同设备UI变形? UI在不同设备上如何适配?

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 3650

poj1789Truck History 最小生成树水题

//n个车牌号,刚开始只有一个车牌,其他车牌都是由一个车牌直接或间接产生 //一个车牌到另一个车牌的产生权值是它们之间的数字不同的个数 //问产生的最小的边权和 //最小生成树 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 2010 ; const int in

ijbuhv的专栏 3030

【AI智能体】Skills 智能体驱动开发从使用到项目实战详解

Skills 智能体驱动开发从使用到项目实战详解

congge 6603

POJ 1789 Truck History

POJ 1789 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 ow...

旺崽的博客 3156

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

(最小生成树)POJ1789Truck History

POJ1789Truck History 题意&思路: 给你n个长度为7的字符串,每两个字符串之间的权值为相同位置字符不同的个数。每个字符串都由另一个字符串变化而来,变化的质量为1/权值和。求最大质量。 其实就是求最小权值和。 模板题。 代码: #include<iostream> #include<stdio.h> #include<stdlib.h>...

Z7784562的博客 218

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

POJ 1789.Truck History

题目:http://poj.org/problem?id=1789 AC代码(C++): #include #include #include #include #include #include #include #include #include #define INF 0xfffffff #define MAXN 2005 using namespace

njyexiong的博客 305

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

POJ1789【CTU Open 2003】Truck History

题目链接:http://poj.org/problem?id=1789 【分析】     最小生成树不解释。 【代码】 #include #include using namespace std; const int maxn=2010,inf=~0U>>1; char s[maxn][10]; int dist[maxn][maxn],d[maxn],vis[max

Zarxdy34 811

【MST-Prime】POJ 1789 Truck History

POJ 1789 Truck History They defined the distance of truck types as the number of positions with different letters in truck type codes.(卡题面,QAQ,读不下去不想读,后来发现就这一句重要,距离就是两个字符串对应位置上不同字母的个数) 然后就是最小生成树的模板了。 ...

L_X_ 300

poj1789 Truck History(Prim算法实现)

poj1789 Truck History 题意大概是这样的:用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。 例如有如下4个编号: aaaaaaa baaaaaa

时刻保持危机感&对知识的渴望 633

POJ - 1789 Truck History解题报告

prim···

薛崇祥的博客 568

【POJ 1789 --- Truck History】prim || kruskal

【POJ 1789 --- Truck History】prim || kruskal 题目来源:点击进入【POJ 1789 — Truck History】 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, o...

来日浅谈的博客 616

poj 1789 Truck History(kruskal算法)

主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值。然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列写的。可是超时啦,不知道为什么。希望有人能够解答。后面用的数组sort排序然后才AC。 code: 数组sort排序AC代码: #include<cstdio>...

aofan9566的博客 213

poj 1789 Truck History

题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, ...

dong12276的博客 187

poj——1789——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 ea

清溪浅水 551

POJ 1789 Truck History(最小生成树)

POJ 1789 Truck History(最小生成树) http://poj.org/problem?id=1789 题意:        用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance

code 976

Truck History poj1789 最小生成树

传送门:poj1789 题意大概:用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。 看懂了题,稍微想想就能转化为求最小生成树。将编号抽象为点,两个编号之间的衍生关系抽象为边,‘距离

WA是一笔财富 355
上一篇: 百练 1105: S-Trees
下一篇: 百练 2749: 分解因数
coding_sun
博客等级 码龄10年 10粉丝 56原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值