当前位置: 首页 > 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.jsqmd.com/news/930250/

相关文章:

  • Arduino声控小车制作指南:从硬件搭建到代码优化的完整实践
  • 最新高效的AI浏览器企业3个核心维度深度横评 - 速递信息
  • 2026永康木门品牌优选,这几家品质靠谱
  • WindowResizer终极指南:深度解析Windows窗口强制调整技术实现原理
  • 南京防水补漏哪家靠谱?2026本地专业防水品牌测评避坑指南 - 吉修匠
  • 量化训练时 fusebn/withbn 简介
  • R.E.P.O. Modding Wiki 中文翻译完成 - 让国内 Mod 开发者也能轻松上手
  • ShawzinBot:MIDI到游戏乐器自动化演奏的跨领域技术融合实践
  • LanzouAPI技术深度解析:云存储直链提取与自动化下载架构设计
  • 如何一次性解决Windows C++运行库问题:VisualCppRedist AIO终极指南
  • 终极SPT-AKI存档编辑器:简单三步掌握离线版塔科夫角色编辑技巧
  • [手写系列]从零到一:Github开源你的第一个项目
  • 废旧光驱改造激光雕刻机:Arduino与A4988驱动CNC制作全攻略
  • Unity3D书页卷曲效果:终极指南与完整实现方案
  • 2026年实测10款降AI率平台推荐:免费与付费全对比,毕业论文淡化AIGC痕迹必看 - 降AI小能手
  • STM32F407驱动TB6612电机模块避坑指南:从静电防护、PWM频率到PCB走线,一个都不能少
  • 跟踪你的机器学习实验
  • 3分钟掌握ChanlunX:零基础实现缠论自动化分析的终极方案
  • 终极raylib游戏开发指南:简单快速构建跨平台游戏的完整教程
  • 英雄联盟Akari助手:3步搞定智能游戏自动化,免费提升你的游戏效率
  • 陌生人之间的防备心理、社交壁垒、阶层差异。
  • AI赋能Linux Shell:自然语言交互与智能命令生成实践
  • MAA明日方舟助手:智能自动化解放玩家双手的技术实现
  • 解密PyMobileDevice3:用Python掌控iOS设备的终极武器
  • 基于树莓派与Flask的智能安防监控机器人全栈开发实战
  • 如何在5分钟内快速制作专业PPT:免费网页版演示文稿工具终极指南
  • Windows Defender终极掌控方案:开源defender-control深度剖析与技术实现
  • 证件照换底色怎么弄?2026方法、软件和在线工具保姆级教程 - 软件小管家
  • UI-TARS桌面应用:5步实现视觉语言模型驱动的GUI自动化革命
  • 解放你的暗黑破坏神2存档:5分钟搭建专业级可视化编辑器