spfa模版
#include<bits/stdc++.h> using namespace std; int n,m;//点边
int beginn; int u[],v[],w[];
int first[],nextt[]; long long dis[];
int book[]; int k2=; void clean()
{
for(int i=;i<=n;i++)
{
dis[i]=;
if(i==beginn) dis[i]=; first[i]=-;
} for(int i=;i<=m;i++)
{
nextt[i]=-;
}
} void make_edge(int _a,int _b,int _c)
{
k2++;
u[k2]=_a;
v[k2]=_b;
w[k2]=_c;
nextt[k2]=first[u[k2]];
first[u[k2]]=k2;
} deque<int> q; int main()
{
cin>>n>>m>>beginn; clean(); for(int i=;i<=m;i++)
{
int a,b,c;
cin>>a>>b>>c;
make_edge(a,b,c);
} book[beginn]=;
q.push_front(beginn); while(!q.empty())
{
int now_node=q.front();
book[now_node]=;
q.pop_front(); int k=first[u[now_node]];
while(k!=-)
{
if(dis[v[k]]>dis[u[k]]+w[k])
{
dis[v[k]]=dis[u[k]]+w[k];
if(book[v[k]]==)
{
book[v[k]]=;
if(!q.empty()&&dis[v[k]]<dis[q.front()])
{
q.push_front(v[k]);
}
else q.push_back(v[k]);
}
}
k=nextt[k];
}
} for(int i=;i<=n;i++)
{
if(i==beginn) cout<<"0 ";
else cout<<dis[i]<<" ";
} cout<<endl;
return ;
}
spfa未调过
spfa模版的更多相关文章
- 2018年长沙理工大学第十三届程序设计竞赛 E小木乃伊到我家(spfa模版)
链接:https://www.nowcoder.com/acm/contest/96/E来源:牛客网 小木乃伊到我家 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...
- HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)
Sightseeing Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 写一下SPFA和迪杰斯特拉的模版。。。第一次写博客,有错请提出哦!
SPFA的模版 #include<bits/stdc++.h> using namespace std; queue <int> q; typedef pair <int ...
- POJ-1860.CurrencyExchange(Spfa判断负环模版题)
本题思路:每完成一次交换之后交换余额多于原钱数则存在正环,输出YES即可. 参考代码: #include <cstdio> #include <cstring> #includ ...
- dijkstra,SPFA,Floyd求最短路
Dijkstra: 裸的算法,O(n^2),使用邻接矩阵: 算法思想: 定义两个集合,一开始集合1只有一个源点,集合2有剩下的点. STEP1:在集合2中找一个到源点距离最近的顶点k:min{d[k] ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- 费用流&网络流模版
费用流模版: #include<cstdio> #include<cstring> #include<queue> using namespace std; ;// ...
- SPFA【模板】单源最短路径
题目传送 https://www.luogu.org/problem/show?pid=3371没啥解释,SPFA纯模版qaq//Gang #include<iostream> #incl ...
- [NOIP2017赛前复习第二期]复赛考试技巧与模版-普及组
考试技巧 1.拿到考卷首先通看题目,按自己感觉的难度排序(普及一般是1-2-3-4了~还是相信出题人不会坑我们的2333) 2.一般来说,普及组前两道题比较简单(大水题啊233~),但是通常坑很多,例 ...
随机推荐
- 广义欧拉降幂(欧拉定理)——bzoj3884,fzu1759
广义欧拉降幂对于狭义欧拉降幂任然适用 https://blog.csdn.net/qq_37632935/article/details/81264965?tdsourcetag=s_pctim_ai ...
- js面试总结3
异步和单线程 题目: 1.同步和异步的区别? 2.一个关于setTimeout的笔试题. 3.前段使用异步的场景有哪些? 什么是异步? console.log(100) setTimeout(func ...
- 在centos 6.9 x64下安装code::blocks步骤
1.yum groupinstall "Development tools" 2.yum install gtk2* 3.安装wxWidgets 下载地址:https://www. ...
- latex ctex 的section不能写中文, /href
问题描述:再使用超链接 /href 后发现section{}不能写入中文,以前是好使的,经过查询验证,需要在引导区里加入 \hypersetup{CJKbookmarks=true} 即可恢复正常.
- Java并发编程中的若干核心技术,向高手进阶!
来源:http://www.jianshu.com/p/5f499f8212e7 引言 本文试图从一个更高的视角来总结Java语言中的并发编程内容,希望阅读完本文之后,可以收获一些内容,至少应该知道在 ...
- 第三周课堂笔记1thand2thand3th
元组 元组是以逗号隔开的 元组有索引有切片,元组是小括号和中括号的集合, 元组中的东西不可修改(小括号内的东西不可被修改,但是小括号里的列表和字典可以被修改) 2. 由内存地址来分 可变数据类 ...
- 数据可视化(matplotilb)
一,matplotilb库(数学绘图库) mat数学 plot绘图 lib库 matplotlib.pyplot(缩写mp)->python 最常用接口 mp.plot(水平坐标,垂直坐标数组 ...
- springcloud系列13 config的客户端使用
config客户端的使用: 也是首先要引入依赖: <dependency> <groupId>org.springframework.cloud</groupId> ...
- Linux 通用数据结构说明
device_driver include/linux/device.h struct device_driver { const char * name; /* 驱动名称 * ...
- Matlab AlexNet 识别花
1. 首先,你要又并行计算的工具箱,在插件选项里面找到,安装即可 2. 下载训练的数据集,采用matlab演示的材料即可 https://matlabacademy-content.mathworks ...