H. Holy Grail(The Preliminary Contest for ICPC Asia Nanjing 2019题解)

发布时间:2026/11/8 22:40:46

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; }
延伸阅读

更多相关文章

2026/11/8 5:03:15

5分钟快速上手:GoldHEN金手指管理器终极使用指南

5分钟快速上手:GoldHEN金手指管理器终极使用指南 【免费下载链接】GoldHEN_Cheat_Manager GoldHEN Cheats Manager 项目地址: https://gitcode.com/gh_mirrors/go/GoldHEN_Cheat_Manager GoldHEN金手指管理器是专为PlayStation 4玩家设计的开源作弊代码管理工…

2026/11/1 1:08:56

304不锈钢耐腐蚀防火门 滨海高盐雾车间防爆防火通道门

304不锈钢耐腐蚀防火门是专为滨海区域、化工厂区、高盐雾潮湿环境研发的专用消防设备,拥有完整消防认证,适配工业车间、防爆通道、厂区楼道、户外消防通道等场景,有效解决普通钢制防火门易生锈、腐蚀、老化变形的难题,是沿海工程项…

2026/11/8 15:17:53

防火推拉窗优势

防火推拉窗是兼具消防防火功能与日常通风采光的特种消防门窗,区别于传统平开防火窗,推拉开启结构设计更贴合现代建筑大洞口、大空间使用需求,广泛应用于厂房、仓库、机房、地下室、商业综合体等场所,综合使用优势十分突出。该窗最…

2026/11/8 22:37:39

宝安电工证中心在哪考?证书过期了不知道怎么复审延期

宝安电工证中心在哪考?证书过期了不知道怎么复审延期 证书过期了不知道怎么复审延期,是不是你最近最头疼的事?别急,今天就给你讲清楚怎么在 宝安电工证中心 报名、考试和复审,还有你关心的 在哪考 问题,一网打尽。 为什么电工证不能随便拖?…

2026/11/8 22:37:39

上海 低压电工证到期换证流程

上海低压电工证到期换证流程:怎么避坑?电子证书怎么查? 你是不是正为上海低压电工证到期换证发愁?不知道去哪报名,怕被中介坑?别急,这篇文章帮你理清楚整个流程,教你如何用电子证书快速换证,不花冤枉钱,不走弯路。…

2026/11/8 22:37:39

应城市电工证补办官方回应:实操考试心里没底怕挂科怎么办

应城市电工证补办官方回应:实操考试心里没底怕挂科怎么办 实操考试心里没底怕挂科,这几乎是每一位准备参加应城市电工证补办的学员的真实写照。尤其是已经工作多年的电工,重新补办证书时,面对考核流程、内容和标准的变化,难免会有焦虑。但别担心,官方回…

2026/11/8 22:32:39

室内电梯安装需要特种工证件多少钱?实操考试心里没底怕挂科怎么办

室内电梯安装需要特种工证件多少钱?实操考试心里没底怕挂科怎么办 实操考试心里没底怕挂科?你不是一个人。尤其是像南阳这样的中小施工企业负责人,每次谈到特种作业证,心里都直打鼓。这不仅关系到项目能不能顺利推进,还直接影响到企业安全合规和员工收入…

2026/11/8 0:00:27

电工证丢失还能补吗?千万别踩坑,丢了证等于失业

电工证丢失还能补吗?千万别踩坑,丢了证等于失业 你正在找工作,但手里的电工证突然丢了,这下怎么办?别急,先别慌, 电工证丢失还能补吗 ?答案是肯定的,但 千万别踩坑 ,补办流程如果不了解,可能会耽误你上岗、甚至影响到你的收入。…

2026/11/8 0:00:27

电工证丢失还能补吗?千万别踩坑,丢了证等于失业

电工证丢失还能补吗?千万别踩坑,丢了证等于失业 你正在找工作,但手里的电工证突然丢了,这下怎么办?别急,先别慌, 电工证丢失还能补吗 ?答案是肯定的,但 千万别踩坑 ,补办流程如果不了解,可能会耽误你上岗、甚至影响到你的收入。…

2026/11/7 14:54:47

面向复杂光照与角度的宠物面部识别鲁棒性增强策略

在智慧养宠时代,基于视觉的宠物身份识别技术正逐步从实验室走向家庭应用。然而,与配合度高的人类不同,宠物是典型的“非合作生物识别对象”——它们不会主动配合摄像头调整姿态,更无法控制光照环境。实际应用中,我们常…

2026/11/7 14:50:12

SSM电子书店源码全解析:结构、原理、部署与排错实战

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

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑