题目链接:点击查看

题目大意:给出一个图,包括n个点和m条边,然后给出6个查询,每次询问给出两个点u和v,输出v到u的最短路的相反数,并将u到v,权值为刚才求出的答案这条边加入到图中

题目分析:裸的最短路,不过有负边权,但是n很小,只有300,用spfa或floyd都可以,当时比赛的时候愣是没读懂题目是什么意思,题目整的花里胡哨的。。也是服了,不过用floyd的时候注意一下,每次重新跑最短路的时候要从初始状态跑,不能从上一次跑完的的矩阵再跑,开个辅助数组保存一下一开始的边权即可,剩下的就是模板题了,俩方法都写了一遍,直接上代码吧:

SPFA:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;const int inf=0x3f3f3f3f;const int N=310;int n,m;int d[N];bool vis[N];int maze[N][N];void spfa(int u)
{memset(d,inf,sizeof(d));memset(vis,false,sizeof(vis));d[u]=0;vis[u]=true;queue<int>q;q.push(u);while(!q.empty()){int temp=q.front();q.pop();vis[temp]=false;for(int i=1;i<=n;i++){if(maze[temp][i]==inf)continue;if(d[i]>d[temp]+maze[temp][i]){d[i]=d[temp]+maze[temp][i];if(!vis[i]){q.push(i);vis[i]=true;}}}}
}int main()
{int w;cin>>w;while(w--){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)maze[i][j]=i==j?0:inf;while(m--){int u,v,w;scanf("%d%d%d",&u,&v,&w);u++;v++;maze[u][v]=w;}for(int i=1;i<=6;i++){int u,v;scanf("%d%d",&u,&v);u++;v++;spfa(v);printf("%d\n",-d[u]);maze[u][v]=-d[u];}} return 0;
}

Floyd:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;const int inf=0x3f3f3f3f;const int N=310;int n,m;int maze[N][N];int main()
{int w;cin>>w;while(w--){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)maze[i][j]=i==j?0:inf;while(m--){int u,v,w;scanf("%d%d%d",&u,&v,&w);u++;v++;maze[u][v]=w;}for(int t=1;t<=6;t++){int u,v;scanf("%d%d",&u,&v);u++;v++;for(int k=1;k<=n;k++){for(int i=1;i<=n;i++){if(maze[i][k]==inf)continue;for(int j=1;j<=n;j++){if(maze[i][j]>maze[i][k]+maze[k][j])maze[i][j]=maze[i][k]+maze[k][j];}}}printf("%d\n",-maze[v][u]);maze[u][v]=-maze[v][u];}} return 0;
}

2019ICPC(南京) - Holy Grail(最短路)相关推荐

  1. 【2019icpc南京站网络赛 - H】Holy Grail(最短路,spfa判负环)

    题干: As the current heir of a wizarding family with a long history,unfortunately, you find yourself f ...

  2. 2019ACM南京网络赛 Holy Grail

    ACM2019南京网络赛 Holy Grail SPFA 或 Bellman-Ford模板题(存在负权路径) Description Input Output 样例输入 样例输出 题目链接: http ...

  3. [ICPC Asia Nanjing 2019] Holy Grail (spfa最短路)

    题意:给出一个n个点,m条边的有向图,再给6组询问,在u,v间建一条权值最小的边,使图中没有负环.保证有解. 做法:u,v加边,如果有负环必定过u,v.以v为起点,到u的最短路和u->v组成的环 ...

  4. Holy Grail

    Holy Grail 限制1000 ms 256 MB As the current heir of a wizarding family with a long history,unfortunat ...

  5. The Preliminary Contest for ICPC Asia Nanjing 2019ICPC南京网络赛

    B.super_log (欧拉降幂) •题意 定一个一个运算log*,迭代表达式为 给定一个a,b计算直到迭代结果>=b时,最小的x,输出对m取余后的值 •思路 $log*_{a}(a^{a}) ...

  6. H - Holy Grail

    H - Holy Grail 题意: 题干又臭又长 我简单说说 n个点,m条有向边,边权为负,然后给你六组起始点(s点和t点),你要在s和t之间建一个有向边,要使得权值最小,问这六组边依次是多少? 不 ...

  7. 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 ...

  8. 2019ICPC南京网络赛A题 The beautiful values of the palace(三维偏序)

    2019ICPC南京网络赛A题 The beautiful values of the palace https://nanti.jisuanke.com/t/41298 Here is a squa ...

  9. 寻找圣杯 In Search of the Holy Grail

    最近在内部讨论关于"完美三栏"的话题,看到一篇"In Search of the Holy Grail",相当的好.故此翻译之. In Search of th ...

最新文章

  1. Ubuntu 12.10 拨号上网及停用方法
  2. mysql参数文件选项组_选项文件(Option Files)/配置文件(Configuration Files)的使用
  3. 打开多个界面_使用 Terminator 在一个窗口中运行多个终端
  4. matlab 显示多幅图像,运用matlab实现循环语句中的多幅图像显示
  5. php审计学习:xdcms2.0.8注入
  6. gc垃圾收集器 与gc算法_GC解释:收集器概述
  7. 【Python爬虫】BeautifulSoup4 库的一些用法
  8. python的property用法_Python的@property使用方法详解
  9. IIS7 大文件上传下载限制设置
  10. java meta-inf作用_java - META-INF的目的是什么?
  11. 计算机二级考试考的什么内容,计算机二级考试内容考些什么
  12. 移动光驱装服务器系统盘,光驱别扔,可以改装成移动光驱用
  13. 最全小说资源网——连载阅读升级版(亲测好用)
  14. 计算机网络量化噪音是怎么消除的,数字图像噪声消除算法研究(可编辑).doc
  15. java web 图片上传
  16. 用AI写代码 -- Github Copilot测试
  17. 保监会欲放险资投房产
  18. 智融SW6206、SW3516、SW3522、SW2303等快充市场方案应用
  19. C++习题06(01)函数模板
  20. Linksys EA6500 V1 刷梅林固件过程记录

热门文章

  1. MySQL sql99语法—左(右)外连接
  2. Nacos源码覆盖实例列表
  3. Nacos-认识和安装Nacos
  4. Zookeeper基于Java 访问
  5. RPC实现Provider服务端业务逻辑
  6. Spring 框架中的单例Beans 是线程安全的么?
  7. jvm_虚拟机参数讲解(三)
  8. 递归 递归的案例 递归的案例
  9. html中加入一个计时器,向html中的计时器添加毫秒
  10. Versions maven plugin 修改版本