H. Holy Grail(The Preliminary Contest for ICPC Asia Nanjing 2019题解)
题目链接As the current heir of a wizarding family with a long history,unfortunately, you find yourself forced to participate in the cruel Holy Grail War which has a reincarnation of sixty years.However,fortunately,you summoned a Caster Servant with a powerful Noble Phantasm.When your servant launch her Noble Phantasm,it will construct a magic field,which is actually adirectedgraphconsisting ofn verticesandm edges.More specifically,the graph satisfies the following restrictions :Does not have multiple edges(for each pair of verticesxandy, there is at most one edge between this pair of vertices in the graph) and does not have self-loops(edges connecting the vertex with itself).May havenegative-weighted edges.Does not have anegative-weighted loop.n300,m500.Currently,as your servants Master,as long as you add extra6edges to the graph,you will beat the other 6 masters to win the Holy Grail.Howeveryou are subject to the following restrictions when you add the edges to the graph:Each time you add an edge whose cost is c,it will cost you c units of Magic Value.Therefore,you need to add an edge which has the lowest weight(its probably that you need to add an edge which has a negative weight).Each time you add an edge to the graph,the graph must not have negative loops,otherwise you will be engulfed by the Holy Grail you summon.InputInput data contains multiple test cases. The first line of input contains integert— the number of testcases (1≤t≤5).For each test case,the first line contains two integers n,m,the number of vertices in the graph, the initial number of edges in the graph.Then m lines follow, each line contains three integersx, y and w(0≤x,yn,-10^9≤w≤10^9, x​y) denoting an edge from vertices x to y (0-indexed) of weight w.Then 6 lines follow, each line contains two integerss,tdenotingthe starting vertexandthe ending vertexof the edge you need to add to the graph.It is guaranteed that there is not an edge starting froms to tbefore you add any edges and there must exists such an edge which has the lowest weight and satisfies the above restrictions, meaning the solution absolutely exists for each query.OutputFor each test case,output 666 lines.Each line contains the weight of the edge you add to the graph.INPUT 1 10 15 4 7 10 7 6 3 5 3 3 1 4 11 0 6 20 9 8 25 3 0 9 1 2 15 9 0 27 5 2 0 7 3 -5 1 7 21 5 0 1 9 3 16 1 8 4 4 1 0 3 6 9 2 1 8 7 0 4 OUTPUT -11 -9 -45 -15 17 7题意n个点m条边给出m条边的起点终点和权值最后给出6组数x,y要求在x到y上加权值问所加权值最小为多少加边满足加完之后不能有负环加完立即生效。思路直接找y到x的最短路取相反值即可因为这样不会有负环。因为有负边所以用SPFA。#includestdio.h #includealgorithm #includeiostream #includeset #includemap #includestring #includestring.h #includemath.h #includevector #includequeue #define maxn 150000 #define ll long long #define inf 0x3f3f3f3f using namespace std; ll dis[500]; int vis[500]; struct A { int to; ll w; }; vector A v[500]; void SPFA(ll s) { queue int q; memset(dis,inf,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[s]0; vis[s]1; q.push(s); while(!q.empty()) { int uq.front(); q.pop(); vis[u]0; for(int i0;iv[u].size();i) { int xv[u][i].to; ll yv[u][i].w; if(dis[u]ydis[x]) { dis[x]dis[u]y; if(vis[x]0) { q.push(x); vis[x]1; } } } } return ; } int main() { int t; scanf(%d,t); while(t--){ int n,m,a,b; ll c; struct A t; scanf(%d%d,n,m); for(int i1;im;i){ scanf(%d %d %lld,a,b,c); t.to b; t.w c; v[a].push_back(t); } for(int i0;i6;i){ scanf(%d %d,a,b); SPFA(b); ll ans dis[a]; t.to b; t.w (-1)*ans; v[a].push_back(t); printf(%lld\n,(-1)*ans); } for(int i0;in;i){ v[i].clear(); } } return 0; }

相关新闻

如何彻底掌控Windows窗口:Window Resizer终极桌面管理指南

如何彻底掌控Windows窗口:Window Resizer终极桌面管理指南

如何彻底掌控Windows窗口:Window Resizer终极桌面管理指南 【免费下载链接】WindowResizer 一个可以强制调整应用程序窗口大小的工具 项目地址: https://gitcode.com/gh_mirrors/wi/WindowResizer Window Resizer是一款开源免费的Windows窗口大小强制调整工具…

2026/10/12 1:21:26 阅读更多 →
L1-015 跟奥巴马一起画方块 (15 分)(JAVA)

L1-015 跟奥巴马一起画方块 (15 分)(JAVA)

美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在…

2026/10/8 1:29:16 阅读更多 →
ZXDoc工业级CAN总线仿真工具全解析与应用实践

ZXDoc工业级CAN总线仿真工具全解析与应用实践

1. ZXDoc工具定位与核心功能解析ZXDoc作为一款工业级总线仿真工具,其核心价值在于打通了从协议解析到人机交互的完整闭环。不同于常规CAN分析仪仅提供数据抓取功能,ZXDoc的仿真能力覆盖了物理层信号模拟、协议层报文交互、应用层面板控制三个维度&#x…

2026/10/9 19:29:49 阅读更多 →

最新新闻

SGI-STL空间配置器源码剖析:两级配置器与内存池设计

SGI-STL空间配置器源码剖析:两级配置器与内存池设计

第一次翻开SGI-STL源码时&#xff0c;大部分人都会在<stl_alloc.h>和<stl_uninitialized.h>面前停下来。这两个文件不像vector、list那样能让你立刻看到数据结构的样子&#xff0c;它们做的是更底层的事&#xff1a;在还没构造任何对象之前&#xff0c;先把裸内存准…

2026/10/13 9:01:55 阅读更多 →
基于 SQL Server 的学生选课系统数据库设计:从 ER 图到并发控制完整指南

基于 SQL Server 的学生选课系统数据库设计:从 ER 图到并发控制完整指南

简介&#xff1a;基于 SQL Server 的学生选课系统数据库设计源码与配套文档&#xff0c;面向需要完成课程设计、期末大作业的高校学生&#xff0c;尤其是 SQL Server 初学者。压缩包共 5 个文件&#xff0c;约 138KB&#xff0c;包含 SQL 脚本、Word 详细设计文档、README 说明…

2026/10/13 9:01:55 阅读更多 →
Halcon HObject 转 OpenCVSharp Mat 零拷贝实战与避坑指南

Halcon HObject 转 OpenCVSharp Mat 零拷贝实战与避坑指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/13 9:01:55 阅读更多 →
SQL Server选课系统数据库设计:事务、约束与并发控制实践

SQL Server选课系统数据库设计:事务、约束与并发控制实践

简介&#xff1a;一套基于SQL Server的学生选课系统数据库设计完整方案&#xff0c;面向数据库课程设计、期末大作业以及需要快速上手数据库项目的初学者。项目内含可执行SQL脚本、详细设计文档、Markdown说明与架构截图&#xff0c;覆盖数据表、关系、存储过程等核心内容&…

2026/10/13 9:01:55 阅读更多 →
WinCC用户归档实战:从配方批次追溯到SQL直连的完整案例

WinCC用户归档实战:从配方批次追溯到SQL直连的完整案例

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/13 9:01:55 阅读更多 →
Java Pattern.compile 原理与性能优化:从编译到复用 Matcher 的实践

Java Pattern.compile 原理与性能优化:从编译到复用 Matcher 的实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/13 9:00:51 阅读更多 →

日新闻

物联网系统集成避坑指南:从选型到验收的关键要点

物联网系统集成避坑指南:从选型到验收的关键要点

做了这么多年物联网系统集成的项目&#xff0c;我最大的感受是&#xff0c;大部分坑不是出在技术前沿&#xff0c;而是出在一些看起来很平常的地方。设备选了便宜的、协议没问清楚、现场没摸透、验收标准写得含糊——这些细枝末节&#xff0c;最后都会变成延期、超支和扯皮的大…

2026/10/13 0:00:57 阅读更多 →
ESP32舵机控制全攻略:从PWM原理到高级玩法与常见故障排查

ESP32舵机控制全攻略:从PWM原理到高级玩法与常见故障排查

如果你玩ESP32有一阵子了&#xff0c;很难不碰到舵机控制这道坎。做智能小车要转方向&#xff0c;做机械臂要抬关节&#xff0c;做云台要偏航俯仰&#xff0c;背后都是同一个东西在干活&#xff1a;舵机。而我见过太多人卡在“代码能编译、舵机就是不听话”这个阶段&#xff0c…

2026/10/13 0:00:57 阅读更多 →
CAN总线原理与STM32实战:抗干扰、仲裁、错误处理全解析

CAN总线原理与STM32实战:抗干扰、仲裁、错误处理全解析

1. 为什么CAN总线至今仍是工业现场的“硬通货”&#xff1f;——从汽车底盘到智能温室的真实需求你有没有拆过一辆老款燃油车的中控台&#xff1f;或者调试过某高校实验室里那台跑着Modbus协议却频频丢帧的PLC控制柜&#xff1f;又或者&#xff0c;在某个农业物联网项目里&…

2026/10/13 0:00:57 阅读更多 →

周新闻

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

简介&#xff1a;基于 ARIMA、LSTM、Transformer 等模型的流感时间序列预测 Python 源码&#xff0c;面向计算机相关专业课程设计与期末大作业学生&#xff0c;以及项目实战学习者。内容覆盖预处理、平稳性检验、定阶、残差分析、多模型对比预测的完整时序建模流程&#xff0c;…

2026/10/13 2:54:56 阅读更多 →
影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程&#xff1a;键盘模拟输入实战——输入文本与模拟按键的区别 做影刀RPA自动化&#xff0c;十个新手有八个栽在"往输入框里填东西"这件事上&#xff1a;要么填不进去&#xff0c;要么填了一半&#xff0c;要么直接把原来内容追加在后面。这背后的根因&…

2026/10/12 0:16:38 阅读更多 →
影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程&#xff1a;阅文起点小说数据采集实战——书籍信息与章节内容 1. 认识影刀&#xff1a;什么场景该用RPA采小说数据 起点中文网的页面结构相对稳定——分类榜单、书籍详情、章节内容三块独立页面&#xff0c;跳转链路清晰。这种场景非常适合影刀自动化&#x…

2026/10/13 1:58:08 阅读更多 →

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/12 21:05:09 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/12 14:48:49 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/12 18:11:17 阅读更多 →