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/7/28 14:09:43 阅读更多 →
L1-015 跟奥巴马一起画方块 (15 分)(JAVA)

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

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

2026/7/28 14:09:43 阅读更多 →
ZXDoc工业级CAN总线仿真工具全解析与应用实践

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

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

2026/7/28 14:09:43 阅读更多 →

最新新闻

计算机毕业设计之基于SpringBoot的电子游戏资讯网站的设计与实现

计算机毕业设计之基于SpringBoot的电子游戏资讯网站的设计与实现

随着中国经济的发展,人民的生活质量逐渐提高,于是对网络的依赖性也越来越高,通过网络处理的事务越来越多。特别是随着移动互联网与大数据时代的到来,更是让人们随时享受着网络和新技术给带来了前所未有的用户体验,但是…

2026/7/28 14:18:50 阅读更多 →
拍照即笔记!用 GPT 视觉大模型提取书本片段并一键生成思维导图实战

拍照即笔记!用 GPT 视觉大模型提取书本片段并一键生成思维导图实战

阅读专业书籍或文献时,遇到精彩片段,手动打字录入不仅效率低,还会打断阅读心流。传统的 OCR 软件虽然能把图片转成文字,却无法帮你理清段落之间的逻辑脉络。如今,借助 neneai.cn 这类 AI 模型聚合平台,阅读…

2026/7/28 14:18:50 阅读更多 →
分布式系统的六个经典脑裂场景:从选举到数据分片的避坑实战

分布式系统的六个经典脑裂场景:从选举到数据分片的避坑实战

分布式系统的六个经典脑裂场景:从选举到数据分片的避坑实战脑裂(Split-Brain)是分布式系统中最棘手的故障模式之一——系统分裂成两个或多个独立子集群,各自认为自己是"主",同时对外提供服务,导致…

2026/7/28 14:18:50 阅读更多 →
计算机毕业设计之基于springboot的订单分发与拆分系统的设计与实现

计算机毕业设计之基于springboot的订单分发与拆分系统的设计与实现

如今,在科学技术飞速发展的情况下,信息化的时代也已因为计算机的出现而来临,信息化也已经影响到了社会上的各个方面。它可以为人们提供许多便利之处,可以大大提高人们的工作效率。随着计算机技术的发展的普及,各个领域…

2026/7/28 14:18:50 阅读更多 →
看图就能写代码!用 GPT 多模态大模型解析算法流程图并生成 Python/Java 实战指南

看图就能写代码!用 GPT 多模态大模型解析算法流程图并生成 Python/Java 实战指南

对于编程初学者和软件工程师来说,看懂复杂的系统架构图和算法流程图(Flowchart)往往比直接读代码还要吃力。如果能把这些图形化的逻辑直接转化为可运行的代码,不仅能极大提升学习效率,还能加速业务落地。目前&#xff…

2026/7/28 14:18:50 阅读更多 →
在Photoshop中无缝集成AI绘画:Comfy-Photoshop-SD终极指南

在Photoshop中无缝集成AI绘画:Comfy-Photoshop-SD终极指南

在Photoshop中无缝集成AI绘画:Comfy-Photoshop-SD终极指南 【免费下载链接】Comfy-Photoshop-SD Download this extension via the ComfyUI manager to establish a connection between ComfyUI and the Auto-Photoshop-SD plugin in Photoshop. https://github.com…

2026/7/28 14:17:49 阅读更多 →

日新闻

告别臃肿!3步让你的暗影精灵笔记本重获新生

告别臃肿!3步让你的暗影精灵笔记本重获新生

告别臃肿!3步让你的暗影精灵笔记本重获新生 【免费下载链接】OmenSuperHub Control Omen laptop performance, fan speeds, and keyboard lighting, and unlock power limits. 项目地址: https://gitcode.com/gh_mirrors/om/OmenSuperHub 你是否也曾为官方Om…

2026/7/28 0:00:43 阅读更多 →
RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

做 RAG 的人应该都踩过这个致命的坑:把几百页的财报、法规、技术手册扔给向量库,问一个具体问题,搜出来的全是沾边但没用的内容 —— 关键信息要么被硬切块拆碎了,要么藏在几十条结果的最下面。语义相似≠真正相关,这个…

2026/7/28 0:00:43 阅读更多 →
抖音视频文案提取工具全指南:免费2026版、手机App、在线工具一网打尽

抖音视频文案提取工具全指南:免费2026版、手机App、在线工具一网打尽

2026年做短视频运营,从抖音上扒文案早就不是偷偷抄笔记的事了。我刚开始做内容的时候,每天刷半小时抖音,手动把爆款视频的口播敲进备忘录,一条2分钟的视频得花十来分钟,碰到语速快的还要反复回听。后来试了一圈工具&am…

2026/7/28 0:00:43 阅读更多 →

周新闻

深度学习道路桥梁裂缝检测系统 道路桥梁裂缝检测数据集 道路桥梁病害识别检测数据集

深度学习道路桥梁裂缝检测系统 道路桥梁裂缝检测数据集 道路桥梁病害识别检测数据集

深度学习道路桥梁裂缝检测系统 数据集6000张 完整源码已标注数据集训练好的模型环境配置教程程序运行说明文档,可以直接使用!系统支持图片、视频、摄像头等多种方式检测裂缝,功能强大实用。 1数据集6000张 8各类别

2026/7/28 12:04:22 阅读更多 →
深度学习YOLO模型如何训练 PUBG 绝地求生目标检测数据集

深度学习YOLO模型如何训练 PUBG 绝地求生目标检测数据集

pubg数据集 精选原图1.42万数据 1.49万标签 无任何重复、算法增强或冗余图像! pubg绝地求生目标检测数据集 1分类:e_body,14905个标签,txt格式 共计14244张图,99%为640*640尺寸图像 适合yolo目标检测、AI训练关键词&am…

2026/7/28 8:29:16 阅读更多 →
Apex英雄目标检测数据集 深度学习框架YOLO如何训练APEX数据集

Apex英雄目标检测数据集 深度学习框架YOLO如何训练APEX数据集

Apex检测数据集数据集详情检测类别: allies enemy tag图片总量:7247张训练集:5139张验证集:1425张测试集:683张标注状态:全部已标注,即拿即用数据格式:支持YOLO格式及其他格式&#…

2026/7/28 5:03:42 阅读更多 →

月新闻