《算法笔记》10.5小节——图算法专题->最小生成树 问题 E: Jungle Roads

题目描述

   

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

输入

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

输出

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

样例输入
3
A 1 B 42
B 1 C 87
6
A 2 B 13 E 55
B 1 C 1
C 1 D 20
D 1 E 4
E 1 F 76
0
样例输出
129
114

题目大意:给出 n 个城市,之后给出 n-1 行信息,第一个字符表示城市的编号 c,之后第一个数字 cnt 表示该城市出发有几条路径。之后给出 cnt 组信息,每组一个字符一个数字,表示从城市 c 到对应城市的长度。求整个图的最小生成树的总长度。

分析:最小生成树模板题。由于给出的是边信息,可以用 kruskal 算法。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;typedef struct node
{int t,dis;
}node;int findFather(int a,int father[])
{int z=a;while(a!=father[a])a=father[a];while(z!=a){int temp=father[z];father[z]=a,z=temp;}return a;
}int kruskal(vector<node>graph[],int n,int father[])
{int ans=0;for(int times=1;times<n;++times){int u=-1,v=-1,mini=INF;for(int i=0;i<n;++i){int len=graph[i].size();int fi=findFather(i,father);for(int j=0;j<len;++j){if(graph[i][j].dis<mini){int fj=findFather(graph[i][j].t,father);if(fi!=fj)u=i,v=j,mini=graph[i][j].dis;}}}if(u==-1)return -1;ans+=graph[u][v].dis;int fu=findFather(u,father);int fv=findFather(graph[u][v].t,father);father[fu]=fv;}return ans;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint n;while(scanf("%d\n",&n),n){vector<node>graph[n+5];int father[n+5];for(int i=0;i<n;++i)father[i]=i;for(int t=1;t<n;++t){int cnt;char c;scanf("%c%d ",&c,&cnt);int index=c-'A';for(int i=0;i<cnt;++i){int d;char cc;scanf("%c%d",&cc,&d);getchar();node temp;temp.t=cc-'A',temp.dis=d;graph[index].push_back(temp);}}int ans=kruskal(graph,n,father);printf("%d\n",ans);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/81044.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【音视频】SDL简介

官网&#xff1a;官网 文档&#xff1a;文档 SDL&#xff08;Simple DirectMedia Layer&#xff09;是一套开放源代码的跨平台多媒体开发库&#xff0c;使用C语言写成。SDL提供数种控制图像、声音、输出入的函数&#xff0c;让开发者只 要用相同或是相似的代码就可以开发出跨多…

SpringBoot + SSE 实时异步流式推送

前言 在当今数字化时代&#xff0c;实时数据处理对于企业的决策和运营至关重要。许多业务场景需要及时响应数据库中的数据变化&#xff0c;例如电商平台实时更新库存、金融系统实时监控交易数据等。 本文将详细介绍如何通过Debezium捕获数据库变更事件&#xff0c;并利用Serv…

ADS1299模拟前端(AFE)代替芯片——LHE7909

在现代医疗科技的飞速发展中&#xff0c;精确的生物电势测量设备变得越来越重要。领慧立芯推出的LHE7909&#xff0c;是一款专为心电图&#xff08;ECG&#xff09;和其他生物电势测量设计的低噪声24位模数转换器&#xff08;ADC&#xff09;&#xff0c;为医疗设备制造商提供了…

如何实现Redis和Mysql中数据双写一致性

一、引言 今天我们来聊聊一个在分布式系统中非常常见但又十分棘手的问题——Redis与MySQL之间的双写一致性。我们在项目中多多少少都遇到过类似的困扰&#xff0c;缓存是用Redis&#xff0c;数据库是用MySQL&#xff0c;但如何确保两者之间的数据一致性呢&#xff1f;接下来我…

面试篇 - Transformer前馈神经网络(FFN)使用什么激活函数?

1. FFN结构分解 原始Transformer的FFN层 FFN(x) max(0, xW₁ b₁)W₂ b₂ # 原始论文公式 输入&#xff1a;自注意力层的输出 x&#xff08;维度 d_model512&#xff09; 扩展层&#xff1a;xW₁ b₁&#xff08;扩展为 d_ff2048&#xff09; 激活函数&#xff1a;Re…

基于Python Flask的深度学习电影评论情感分析可视化系统(2.0升级版,附源码)

博主介绍&#xff1a;✌IT徐师兄、7年大厂程序员经历。全网粉丝15W、csdn博客专家、掘金/华为云//InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;&#x1f3…

前端vue2修改echarts字体为思源黑体-避免侵权-可以更换为任意字体统一管理

1.下载字体 npm install fontsource/noto-sans-sc 不知道为什么我从github上面下载的不好使&#xff0c;所以就用了npm的 2.引用字体 import fontsource/noto-sans-sc; 在入口文件-main.js中引用 3.设置echats模板样式 import * as echarts from echarts; // 在import的后…

51c自动驾驶~合集37

我自己的原文哦~ https://blog.51cto.com/whaosoft/13878933 #DETR->DETR3D->Sparse4D 走向长时序稀疏3D目标检测 一、DETR 图1 DETR架构 DETR是第一篇将Transformer应用到目标检测方向的算法。DETR是一个经典的Encoder-Decoder结构的算法&#xff0c;它的骨干网…

【MongoDB篇】MongoDB的集合操作!

目录 引言第一节&#xff1a;集合的“诞生”——自动出现还是手动打造&#xff1f;&#x1f914;第二节&#xff1a;集合的“查阅”——看看这个数据库里有哪些柜子&#xff1f;&#x1f4c2;&#x1f440;第三节&#xff1a;集合的“重命名”——给文件柜换个名字&#xff01;…

Goland终端PowerShell命令失效

Goland终端Terminal的PowerShell不能使用&#xff0c;明明windows上升级了PowerShell 7设置了配置文件&#xff0c;但是只能在windows终端下使用&#xff0c;goland终端下直接失效报错&#xff0c;安装升级PowerShell请看Windows11终端升级PowerShell7 - HashFlag - 博客园 问…

简单分析自动驾驶发展现状与挑战

一、技术进展与市场渗透 技术分级与渗透率 当前量产乘用车的自动驾驶等级以L2为主&#xff08;渗透率约51%&#xff09;&#xff0c;L3级处于初步落地阶段&#xff08;渗透率约20%&#xff09;&#xff0c;而L4级仍处于测试和示范运营阶段&#xff08;渗透率约11%&#xff09;2…

【C++类和数据抽象】消息处理示例(1):从设计模式到实战应用

目录 一、数据抽象概述 二、消息处理的核心概念 2.1 什么是消息处理&#xff1f; 2.2 消息处理的核心目标 三、基于设计模式的消息处理实现 3.1 观察者模式&#xff08;Observer Pattern&#xff09; 3.2 命令模式&#xff08;Command Pattern&#xff09; 四、实战场景…

【统计方法】交叉验证:Resampling, nested 交叉验证等策略 【含R语言】

Resampling (重采样方法) 重采样方法是从训练数据中反复抽取样本&#xff0c;并在每个&#xff08;重新&#xff09;样本上重新调整模型&#xff0c;以获得关于拟合模型的附加信息的技术。 两种主要的重采样方法 Cross-Validation (CV) 交叉验证 &#xff1a; 用于估计测试误…

常见的 CSS 知识点整理

1. 盒模型&#xff08;Box Model&#xff09;是什么&#xff1f;标准盒模型和 IE 盒模型的区别&#xff1f; 答案&#xff1a; CSS 盒模型将元素视为一个盒子&#xff0c;由内容&#xff08;content&#xff09;、内边距&#xff08;padding&#xff09;、边框&#xff08;bor…

Educational Codeforces Round 178 div2(题解ABCDE)

A. Three Decks #1.由于最后三个数会相等&#xff0c;提前算出来和&#xff0c;%3判断&#xff0c;再判前两个数是否大于 #include<iostream> #include<vector> #include<stdio.h> #include<map> #include<string> #include<algorithm> #…

如何创建一个导入模板?全流程图文解析

先去找到系统内可以上传东西的按钮 把你的模板上传上去,找到对应的fileName 图里的文字写错了,是复制粘贴"filePath"到URL才能下载

通信原理第七版与第六版区别附pdf

介绍 我用夸克网盘分享了「通信原理 第7版》樊昌信」&#xff0c;链接&#xff1a;https://pan.quark.cn/s/be7c5af4cdce 《通信原理&#xff08;第7版&#xff09;》是在第6版的基础上&#xff0c;为了适应当前通信技术发展和教学需求&#xff0c;并吸取了数十所院校教师的反…

Mysql唯一性约束

唯一性约束&#xff08;Unique Constraint&#xff09;是数据库设计中用于保证表中某一列或多列组合的值具有唯一性的一种规则。它可以防止在指定列中插入重复的数据&#xff0c;有助于维护数据的完整性和准确性。下面从几个方面为你详细解释 作用 确保数据准确性&#xff1a…

测试基础笔记第十六天

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 一、UI自动化介绍1.认识UI自动化测试2.实施UI自动化测试前置条件3.UI自动化测试执行时机4.UI自动化测试核心作用和劣势 二、认识Web自动化测试工具-Selenium021.Sel…

PaddleX的安装

参考&#xff1a;安装PaddlePaddle - PaddleX 文档 1、安装PaddlePaddle 查看 docker 版本 docker --version 若您通过 Docker 安装&#xff0c;请参考下述命令&#xff0c;使用飞桨框架官方 Docker 镜像&#xff0c;创建一个名为 paddlex 的容器&#xff0c;并将当前工作目…