当前位置: 首页 > news >正文

题解:AtCoder AT_awc0080_d Network Construction

本文分享的必刷题目是从蓝桥云课洛谷AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。

欢迎大家订阅我的专栏:算法题解:C++与Python实现!

附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总


【题目来源】

AtCoder:D - Network Construction

【题目描述】

Takahashi is the network administrator of a data center withN NNservers. The servers are numbered from1 11toN NN.

There areM MMavailable cables for connecting servers, and the cables are also numbered from1 11toM MM. Thei ii-th cable( 1 ≤ i ≤ M ) (1 \leq i \leq M)(1iM)bidirectionally connects serverU i U_iUiand serverV i V_iVi, with an installation cost ofW i W_iWi. Each cable connects two distinct servers (i.e.,U i ≠ V i U_i \neq V_iUi=Vi). However, there may be multiple cables connecting the same pair of servers.

Takahashi wants to select some cables to build a network so that allN NNservers can communicate with each other. The set of selected cables must form aspanning tree. A spanning tree is a graph that includes allN NNservers as vertices, where all servers are connected by the selected cables, and no cycles exist (in this case, exactlyN − 1 N-1N1cables are selected).

However, due to security requirements, a specific set ofK KKcables (numberedE 1 , E 2 , … , E K E_1, E_2, \dots, E_KE1,E2,,EK) have already been contracted as dedicated encrypted communication lines and must be included in the network.

If a spanning tree exists that contains allK KKspecified cables, output the minimum total installation cost of theN − 1 N-1N1cables included in that spanning tree. If no such spanning tree exists, output− 1 -11.

高桥是一个数据中心的网络管理员,该中心有N NN台服务器。服务器编号为1 11N NN

M MM条可用的电缆用于连接服务器,电缆也编号为1 11M MM。第i ii条电缆(1 ≤ i ≤ M 1 \leq i \leq M1iM)双向连接服务器U i U_iUi和服务器V i V_iVi,安装成本为W i W_iWi。每条电缆连接两台不同的服务器(即U i ≠ V i U_i \neq V_iUi=Vi)。但是,同一对服务器之间可能有多个电缆连接。

高桥希望选择一些电缆来构建一个网络,以便所有N NN台服务器可以相互通信。选中的电缆集合必须构成一棵生成树。生成树是一个图,以所有N NN台服务器为顶点,其中所有服务器通过选中的电缆连接,且不存在环(在这种情况下,恰好选中N − 1 N-1N1条电缆)。

然而,由于安全要求,一组特定的K KK条电缆(编号为E 1 , E 2 , … , E K E_1, E_2, \dots, E_KE1,E2,,EK)已经签约作为专用加密通信线路,必须包含在网络中。

如果存在一棵包含所有K KK条指定电缆的生成树,则输出该生成树中包含的N − 1 N-1N1条电缆的最小总安装成本。如果不存在这样的生成树,输出− 1 -11

【输入】

N NNM MMK KK
U 1 U_1U1V 1 V_1V1W 1 W_1W1
U 2 U_2U2V 2 V_2V2W 2 W_2W2
⋮ \vdots
U M U_MUMV M V_MVMW M W_MWM
E 1 E_1E1E 2 E_2E2… \dotsE K E_KEK

  • The first line contains the number of serversN NN, the number of available cablesM MM, and the number of cables that must be includedK KK, separated by spaces.
  • From the 2nd line to the( M + 1 ) (M + 1)(M+1)-th line, the information of each cable is given overM MMlines.
  • The( 1 + i ) (1 + i)(1+i)-th line indicates that thei ii-th cable connects serverU i U_iUiand serverV i V_iViwith an installation cost ofW i W_iWi.
  • IfK ≥ 1 K \geq 1K1, the( M + 2 ) (M + 2)(M+2)-th line contains the cable numbersE 1 , E 2 , … , E K E_1, E_2, \dots, E_KE1,E2,,EKthat must be included in the network, separated by spaces. IfK = 0 K = 0K=0, this line does not exist.

【输出】

If a spanning tree exists that contains allK KKspecified cables, output in one line the minimum total installation cost of theN − 1 N-1N1cables included in that spanning tree. If no such spanning tree exists, output− 1 -11.

【输入样例】

4 5 1 1 2 3 2 3 1 1 3 2 3 4 4 2 4 5 1

【输出样例】

8

【算法标签】

#生成树

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=200005,M=200005;intn,m,k;// n: 节点数,m: 边数,k: 必须包含的边数intp[N];// 并查集数组structEdge{inta,b,w;// 边的两个端点和权重boolflag;// 标记是否为必须包含的边}edges[M];booloperator<(Edge x,Edge y)// 重载小于运算符,用于排序{if(x.flag!=y.flag)// 优先排序必须包含的边returnx.flag>y.flag;returnx.w<y.w;// 其次按权重排序}intfind(intx)// 并查集的查找函数{if(p[x]!=x)// 路径压缩p[x]=find(p[x]);returnp[x];}intkruskal()// 最小生成树算法{sort(edges+1,edges+m+1);// 排序边for(inti=1;i<=n;i++)// 初始化并查集p[i]=i;intres=0,cnt=0;// res: 总权重,cnt: 已选择的边数for(inti=1;i<=m;i++)// 遍历所有边{inta=edges[i].a,b=edges[i].b,w=edges[i].w;boolisRequired=edges[i].flag;a=find(a),b=find(b);if(a!=b)// 如果两个节点不在同一集合{p[a]=b;// 合并集合res+=w;// 累加权重cnt++;// 边数加1}elseif(isRequired)// 如果边必须包含但形成环return-1;// 返回-1表示无解}if(cnt<n-1)// 如果边数不足n-1return-1;// 返回-1表示无解returnres;// 返回最小生成树的总权重}signedmain(){cin>>n>>m>>k;// 输入节点数、边数和必须包含的边数for(inti=1;i<=m;i++)// 输入边的信息{cin>>edges[i].a>>edges[i].b>>edges[i].w;edges[i].flag=false;// 初始化为非必须}for(inti=1;i<=k;i++)// 标记必须包含的边{inte;cin>>e;edges[e].flag=true;}cout<<kruskal()<<endl;// 输出最小生成树的总权重return0;}

【运行结果】

4 5 1 1 2 3 2 3 1 1 3 2 3 4 4 2 4 5 1 8
http://www.rkmt.cn/news/1441581.html

相关文章:

  • Arduino声控小车制作指南:从硬件搭建到代码优化的完整实践
  • ShawzinBot:MIDI到游戏乐器自动化演奏的跨领域技术融合实践
  • LanzouAPI技术深度解析:云存储直链提取与自动化下载架构设计
  • 如何一次性解决Windows C++运行库问题:VisualCppRedist AIO终极指南
  • 废旧光驱改造激光雕刻机:Arduino与A4988驱动CNC制作全攻略
  • 2026年实测10款降AI率平台推荐:免费与付费全对比,毕业论文淡化AIGC痕迹必看 - 降AI小能手
  • AI赋能Linux Shell:自然语言交互与智能命令生成实践
  • 基于树莓派与Flask的智能安防监控机器人全栈开发实战
  • 如何在5分钟内快速制作专业PPT:免费网页版演示文稿工具终极指南
  • Windows Defender终极掌控方案:开源defender-control深度剖析与技术实现
  • Python实战:构建棒球FIP计算器,量化投手真实表现
  • 5分钟学会用通达信缠论插件:让复杂理论变成简单交易信号
  • 原根、模数与蝴蝶变换:深入理解NTT(快速数论变换)的数学基石与代码实现
  • GOA优化MLP提升入侵检测:群智能算法与神经网络的网络安全实践
  • Path of Building PoE2:流放之路2构建规划的终极免费指南
  • 如何快速实现暗黑2重制版多开:D2RML完整指南
  • Tinkercad Circuits:零成本机器人教学与虚拟仿真的工程实践指南
  • 小米手表表盘设计终极指南:Mi-Create免费可视化工具快速上手教程
  • BetterNCM安装器:3步让网易云音乐变身全能播放器
  • 无线网卡监听模式全解析:从Managed到Monitor,你的网卡到底能干嘛?
  • UE5 CesiumForUnreal避坑指南:从加载本地倾斜模型到解决Sequence卡顿的实战记录
  • Arduino SPI驱动ILI9488触摸屏与轻量级GUI库设计实践
  • Sora 2驱动虚拟偶像视频量产:从模型微调、动捕对齐到实时渲染的7个工业级技术栈实操手册
  • Arduino驱动伺服电机:从PWM原理到电位器实时控制实践
  • TikTok 2026 NG OA 全真题复盘|四道题难度递进,Teleport Labyrinth 翻车率最高
  • 冒险岛游戏编辑终极指南:一站式.wz文件与地图编辑解决方案
  • 基于Micro:bit的声控手机定位器:双击拍手检测算法与嵌入式实践
  • ITSM现代化转型:从成本中心到战略引擎的核心架构与实践
  • OmenSuperHub:释放惠普暗影精灵游戏本全部潜力的开源控制中心
  • 4个核心模块深度解析:Pearcleaner如何实现macOS应用的彻底清理