#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int cnt,st,ed,s,t,w,h[10005],dis[10005],stack[10005];
int q[40005];
struct Node{
int to,w,next;
}edge[10005];//10005,1005
void add(int s,int t,int v)
{
cnt++;
edge[cnt].to=t;
edge[cnt].w=v;
edge[cnt].next =h[s];
h[s]=cnt;
}
int spfa()
{
int head=1,tail=1;
q[1]=st;
while(head<=tail)
{
int u=q[head];
for(int i=h[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].w+dis[u]<dis[v])
{
dis[v]=edge[i].w+dis[u];
if(!stack[v])
{
stack[v]=1;
tail++;
q[tail]=v;
}
}
}
head++;
stack[u]=0;//退出栈
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&s,&t,&w);
add(s,t,w);
}
scanf("%d%d",&st,&ed);//起点,终点
memset(dis,1,sizeof dis);
dis[st]=0;
spfa();
cout<<dis[ed]<<endl;
}

spfa(模板)的更多相关文章

  1. SPFA模板 Bellmanford优化版

    SPFA模板: queue<int>Q; ]; ],sumv[]; *],__next[*],e,w[*],first[],cnts[]; void AddEdge(int U,int V ...

  2. floyed dij spfa 模板

    /* SPFA模板 */ const int inf=0x3f3f3f3f; inline int SPFA(int s){ memset(dis,inf,sizeof(dis)); queue< ...

  3. spfa模板

    通过stl的queue实现的spfa(vector实现邻接表存图) 本模板没有考虑存在两点不连通的情况 如果需要判断则需要用到并查集或者遍历整个邻接表 #include<iostream> ...

  4. spfa(模板)

    spfa作为图论中的常用算法,深受各类出题人和各位OIer的喜爱: so,为了给大众创造福利,宝宝在此奉上spfa大发的思路和模板:以感谢社会, 感谢CCF,感谢CCTV, 感谢我的老师,感谢同学们, ...

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

    一.算法步骤 建立一个队列,初始时队列里只有起始点,再建立一个数组记录起始点到所有点的最短路径(该数组的初始值要赋为极大值,该点到它本身的路径赋为0,下面的模板中该数组为dist[]).然后执行松弛操 ...

  6. hdu-2544-最短路(SPFA模板)

    题目链接 题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa Dijkstra链接 Floyd链接 Bellman-Ford链接 SPFA ...

  7. spfa模板+讲解

    zz http://blog.sina.com.cn/s/blog_6ad20aef0100mc1a.html Spfa算法 (模板源代码) 这是Bellman Ford的改进算法.    算法介绍: ...

  8. 图论--最短路--SPFA模板(能过题,真没错的模板)

    [ACM常用模板合集] #include<iostream> #include<queue> #include<algorithm> #include<set ...

  9. UVA 558 判定负环,spfa模板题

    1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...

  10. Dijkstra堆优化与SPFA模板

    Dijkstra+优先队列 #include<cstdio> #include<cctype> #include<queue> #include<cstrin ...

随机推荐

  1. 编写现代 CSS 代码的 20 个建议

    明白何谓Margin Collapse 不同于其他很多属性,盒模型中垂直方向上的Margin会在相遇时发生崩塌,也就是说当某个元素的底部Margin与另一个元素的顶部Margin相邻时,只有二者中的较 ...

  2. Android逆向之旅---Android应用的汉化功能(修改SO中的字符串内容)

    一.前言 今天我们继续来讲述逆向的知识,今天我们来讲什么呢?我们在前一篇文章中介绍了关于SO文件的格式,今天我们继续这个话题来看看如何修改SO文件中的内容,看一下我们研究的主题: 需求:想汉化一个Ap ...

  3. paper 167:GPU的使用Theano之tutorial

    Theano之使用GPU 英文版本:http://deeplearning.net/software/theano/tutorial/using_gpu.html          using the ...

  4. 转载:IDEA配置SVN及使用

    转自:https://blog.csdn.net/zwj1030711290/article/details/80687365 1.安装svn客户端 之前用myEcplise只需要插件,现在IDEA需 ...

  5. 判断PC或者是APP

    function isPC() { if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { return fa ...

  6. 【Angular】No component factory found for ×××.

    报错现象: 用modal打开某个组件页面时报错 报错:No component factory found for UpdateAuthWindowComponent. Did you add it ...

  7. 服务端:WCF服务层安全检查核心类

    using System.Data; using CSFrameworkV4_5.Common; using CSFrameworkV4_5.Core.SystemSecurity; using CS ...

  8. JavaScript 模拟后台任务

    读书笔记,请勿转载,发布,产权不归我所有,归以前作者所有,我只是做读书笔记. /*! * Copyright 2015 Google Inc. All rights reserved. * * Lic ...

  9. springCloud的使用07-----消息总线(spring cloud bus)

    spring cloud bus 将分布式的节点用轻量的消息代理连接起来.可用于广播配置文件的更改或服务之间的通讯,也可以用于监控. spring cloud bus 默认只支持rabbitmq和ka ...

  10. nowcoder A hard problem /// 数位DP

    题目大意: 称一个数x的各个数位之和为f(x) 求区间L R之间 有多少个数x%f(x)==0 #include <bits/stdc++.h> using namespace std; ...