代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxv=1e4+10;
const int maxe=5e5+10;
const int inf=0x3f3f3f3f; inline int read(){
int x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
} struct node{
int nxt,to,w;
}e[maxe];
int tot=1,head[maxv]; inline void add_edge(int from,int to,int w){
e[++tot]=node{head[from],to,w},head[from]=tot;
} int n,m,st,dis[maxv]; void read_and_parse(){
n=read(),m=read(),st=read();
for(int i=1,from,to,w;i<=m;i++){
from=read(),to=read(),w=read();
add_edge(from,to,w);
}
} bool in[maxv]; void spfa(){
memset(dis,0x3f,sizeof(dis));
queue<int> q;
q.push(st),in[st]=1,dis[st]=0;
while(q.size()){
int u=q.front();q.pop();in[u]=0;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,w=e[i].w;
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
if(!in[v])q.push(v);
}
}
}
for(int i=1;i<=n;i++){
if(dis[i]==inf)printf("2147483647 ");
else printf("%d ",dis[i]);
}
puts("");
} int main(){
read_and_parse();
spfa();
return 0;
}

【模板】spfa的更多相关文章

  1. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  2. 最短路模板[spfa][dijkstra+堆优化][floyd]

    借bzoj1624练了一下模板(虽然正解只是floyd) spfa: #include <cstdio> #include <cstring> #include <alg ...

  3. 最短路算法模板--SPFA

    初见SPFA时,直接认成了优先队列优化的Dijkstra,经过几位大佬的指点,我终于明白了他们的差异. Dijkstra是保证已经出队过的点不再入队,SPFA是已经在队列中不再入队.比较起来,SPFA ...

  4. 基础最短路(模板 spfa)

    Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...

  5. 初识费用流 模板(spfa+slf优化) 餐巾计划问题

    今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...

  6. 最短路算法模板SPFA、disjkstra、Floyd

    朴素SPFA(链表建边) #include <iostream> #include <cstdio> #include <cstring> #include < ...

  7. 【luogu P3371 单源最短路径 】 模板 SPFA优化

    无优化:500ms deque优化:400ms #include <queue> #include <cstdio> #include <cstring> #inc ...

  8. 【luogu P3371 单源最短路径】 模板 SPFA

    题目链接:https://www.luogu.org/problemnew/show/P3371 我永远都喜欢Flyod.dijkstra + heap.SPFA #include <cstdi ...

  9. 【费用流】【Next Array】费用流模板(spfa版)

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...

  10. 模板 - SPFA

    SPFA可以用来判断负环或者计算带负权的最短路. 其实带负权的最短路可以用带势Dijkstra计算-- 所以SPFA基本就拿来判负环了-- #include<bits/stdc++.h> ...

随机推荐

  1. Python练习-8

    1,复习 ascii:字母,数字,特殊字符:1个字节,8位 Unicode:16位 两个字节 升级 32 位 四个字节 utf-8:最少一个字节 8位表示. 英文字母 8位 1个字节 欧洲16位,2个 ...

  2. PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  3. B. Equations of Mathematical Magic

    思路 打表找规律,发现结果是,2的(a二进制位为1总数)次方 代码 #include<bits/stdc++.h> using namespace std; #define ll long ...

  4. Scrum Meeting NO.9

    Scrum Meeting No.9 1.会议内容 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 代码重构:前端通讯模块改为HttpClient+Json √ 2 "我" ...

  5. 单工程搭建springmvc+spring+mybatis(maven,idea)

    单工程搭建springmvc+spring+mybatis(maven,idea) 1.pom.xml <properties> <project.build.sourceEncod ...

  6. CMake系列之二:入门案例-单个源文件

    编写一个源码文件 如下 #include<stdio.h> #include<stdlib.h> double power(double base,int exponent) ...

  7. debug网页时小问题The source attachment does not contain the source for the file

    第一次debug总是出现下图问题,提示我没加源码... The source attachment does not contain the source for the file ... 解决方法: ...

  8. JSON数据格式解析

    JSON数据的语法规则 1.数据以键值对的形式 2.数据由逗号分隔 3.花括号保存对象 4.方括号保存数组 以PHP的数组为例: <?php $arr = array( "aaaa&q ...

  9. Web接口测试-HttpClient

    要实现Web接口测试的自动化有许多方式,比如利用Jmeter.Loadrunner等测试工具都能够实现接口的自动化测试,我们也可以利用一些开源的框架来实现接口的自动化测试,比如我们现在要说的这个Htt ...

  10. centos7切换gnome3桌面与gnome经典桌面

    登陆界面选择设置按钮,选择gnome,进行登陆