vijosP1053 Easy sssp
vijosP1053 Easy sssp
链接:https://vijos.org/p/1053
【思路】
SPFA。
题目中的陷阱比较多,但是只要中规中矩的写SPFA诸如:s与负圈不相连,有重边的情况都可以解决。
需要注意的有:
1、 数据d用long long
2、 如果没有负圈一个节点被更新的次数不会太多,将判断负圈达到改为n/2
跑两遍SPFA,一遍检查负圈,一遍求最短路即可。
【代码】
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<iostream> 5 using namespace std; 6 7 typedef long long LL; 8 const int maxn = 1000+10 , maxm=100000+10; 9 const LL INF=1e15; 10 struct Edge{ 11 int v,w,next; 12 }e[maxm]; 13 int en=-1,front[maxn]; 14 15 int n,m,s; 16 17 inline void AddEdge(int u,int v,int w) { 18 en++; e[en].v=v; e[en].w=w; e[en].next=front[u]; front[u]=en; 19 } 20 21 LL d[maxn]; 22 bool SPFA(int s,int f) { 23 int inq[maxn],cnt[maxn]; 24 queue<int> q; 25 memset(inq,0,sizeof(inq)); 26 memset(cnt,0,sizeof(cnt)); 27 for(int i=1;i<=n;i++) d[i]=INF; 28 29 if(f==1) { 30 for(int i=1;i<=n;i++) { 31 d[i]=0; inq[i]=1; q.push(i); 32 } 33 } 34 else { 35 d[s]=0; inq[s]=1; q.push(s); 36 } 37 while(!q.empty()) { 38 int u=q.front(); q.pop(); inq[u]=0; 39 for(int i=front[u];i>=0;i=e[i].next) { 40 int v=e[i].v , w=e[i].w; 41 if(d[v]>d[u]+w) { 42 d[v]=d[u]+w; 43 if(!inq[v]) { 44 inq[v]=1; 45 q.push(v); 46 if(++cnt[v]>=(n/2)) return true; 47 //猥琐地只判断到n/2 48 } 49 } 50 } 51 } 52 return false; 53 } 54 55 int main() { 56 ios::sync_with_stdio(false); 57 memset(front,-1,sizeof(front)); 58 scanf("%d%d%d",&n,&m,&s); 59 int u,v,w; 60 for(int i=0;i<m;i++) { 61 scanf("%d%d%d",&u,&v,&w); 62 AddEdge(u,v,w); 63 } 64 if(SPFA(0,1)) printf("-1\n"); 65 else 66 { 67 SPFA(s,2); 68 for(int i=1;i<=n;i++) 69 if(d[i]>=INF) printf("NoPath\n"); 70 else printf("%lld\n",d[i]); 71 } 72 return 0; 73 }
vijosP1053 Easy sssp相关推荐
- 《信息学奥赛一本通》提高版题单
第一部分 基础算法 第 1 章 贪心算法 #10000 「一本通 1.1 例 1」活动安排 #10001 「一本通 1.1 例 2」种树 #10002 「一本通 1.1 例 3」喷水装置 #10003 ...
- LOJ 一本通一句话题解系列:
第一部分 基础算法 第 1 章 贪心算法 1):「一本通 1.1 例 1」活动安排:按照结束时间排序,然后扫一遍就可以了. 2):「一本通 1.1 例 2」种树:首先要尽量的往区间重叠的部分种树,先按 ...
- 一本通提高篇在线提交地址
一本通提高篇 1 基础算法 1.1 贪心算法 1.1.1 P2018 [第一章例题1.1]活动安排正确: 9 提交: 17 比率: 52.94 % 1.1.2 P2021 [第一章例题1.2]种树正 ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...
最新文章
- nefu 628 Garden visiting
- python基础(二)字符串內建函数详解
- java10支持mybatis_写了10年的代码,我最怕写Mybatis这些配置,现在有详解了
- 病毒的手工排除与分析(更新完毕)
- 一起学习C语言:函数(四)
- Kali Linux 无线渗透测试入门指南 第六章 攻击客户端
- Spring MVC 实现文件的上传和下载
- OpenCV读取网络摄像头视频并保存到本地
- 获取计算机用户名称的方法,javascript读取用户名和计算机名
- 腾讯天龙八部手游服务器账号上线,天龙八部手游服务器的注册已达到上限 服务器注册上限怎么解决...
- 如何入门网络爬虫,摸索一年的心里路程
- [C++]实现memcpy
- Android插件化初识
- 为什么要在油气行业中应用 IoT?这 8 个应用场景告诉你 IoT 在油气行业中可以做什么...
- Vscode同步扩展插件
- 一看就懂→专票电子化的三条实现路径
- 悟了,电商的千人千面系统,原来还可以这么搞
- 温德姆酒店集团迁移至亚马逊云 应对后疫情旅游高峰
- Linux搭建Nextcloud,打造属于您的专属网盘
- sinatra源码解读
热门文章
- Linux安装及卸载Nginx
- Xilinx ISE 查看原语
- 解决 Oracle column ambiguously defined 的问题
- 关于Unity渲染优化,你可能遇到这些问题
- java 日志开发规范
- 计算机毕业设计健康营养管理系统ssh框架课程设计【代码讲解+安装调试+文档指导】
- 20172305 2018-2019-1 《Java软件结构与数据结构》第六周学习总结
- TFN FT-7光纤熔接机 高性价比的干线机器 性能不输进口机器!
- 科学计算机法后保留四位小数,四舍五入(一种精确度的计数保留法)_百度百科...
- 牛客网——矩阵相等判定