#include<iostream>
using namespace std;
const int nMax = 30005;
const int mMax = 150005;
const int inf = 1000000000; struct node{
int v, w, next;
}edge[mMax];
int n, edgeHead[nMax], dict[nMax];
int stack[nMax];
bool vis[nMax]; void spfa(){
for(int i = 2; i <= n; i ++)
dict[i] = inf;
dict[1] = 0;
int top = 0; // spfa的堆栈实现模板。
stack[++ top] = 1;
vis[1] = true;
while(top){
int u = stack[top --];
for(int p = edgeHead[u]; p != 0; p = edge[p].next){
int v = edge[p].v;
if(dict[v] > dict[u] + edge[p].w){
dict[v] = dict[u] + edge[p].w;
if(!vis[v]){
vis[v] = true;
stack[++ top] = v;
}
}
}
vis[u] = false;
}
} int main(){
int m, i;
scanf("%d%d", &n, &m);
int k = 1;
while(m --){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
edge[k].v = v;
edge[k].w = w;
edge[k].next = edgeHead[u];
edgeHead[u] = k ++;
}
spfa();
printf("%d\n", dict[n]);
return 0;
}

POJ 3159 最短路 SPFA的更多相关文章

  1. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

  2. POJ 1511 最短路spfa

    题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...

  3. poj 3013 最短路SPFA算法

    POJ_3013_最短路 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23630 ...

  4. It&#39;s not a Bug, It&#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

    Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Su ...

  5. POJ 3159 Candies(spfa、差分约束)

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

  6. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  7. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  8. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  9. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

随机推荐

  1. try git

    Git allows groups of people to work on the same documents (often code) at the same time, and without ...

  2. ios -生成推广海报

    #import "ViewController.h" #import "Masonry.h" @interface ViewController () @end ...

  3. 巨蟒python全栈开发django13:中间件部分

    1.回顾昨日内容 2.session认证装饰器 3.django整个流程 4.中间件简单应用 5.简单统计访问次数 6.中间件其他方法 7.orm单表内容回顾

  4. Storm 集群安装

    http://archive.apache.org/dist/storm/  版本都在这 本人安装的是 其他版本的自行安装吧,估计都差不多 sudo mkdir /export/serverssudo ...

  5. <2013 12 01> 一篇很好的关于windows编程的入门指导(2013年末写的,比较前沿)

    我之前做了不少嵌入式开发,从单片机到ARM到RTOS到Linux等等,可以说走的是电气工程师的路线,对编程也是实用性的,跟计算机学院的科班套路不同.最近同学做一个windowsCE的项目请我帮忙,之前 ...

  6. JS续

    JS中的事件 [JS中的事件分类] * 1.鼠标事件: * click/dbclick/mouseover/mouseout/mousemove/mousedown/mouseup * * 2.键盘事 ...

  7. django项目部署在Apache服务器中,静态文件路径的注意点

    django Apache部署静态文件的路径注意点 静态文件放在对应的 app 下的 static 文件夹中 或者 STATICFILES_DIRS 中的文件夹中. 当 DEBUG = True 时, ...

  8. 1.Configure the mongo Shell-官方文档摘录

    Customize the Prompt 自定义提示 You may modify the content of the prompt by setting the variable prompt i ...

  9. MySQL中B+树索引的使用

    1)         不同应用中B+树索引的使用 对于OLTP应用,由于数据量获取可能是其中一小部分,建立B+树索引是有异议时的 对OLAP应用,情况比较复杂,因为索引的添加应该是宏观的而不是微观的. ...

  10. 运行jupyter notebook 出错 Error executing Jupyter command 'notebook'

    实际上是安装jupyter时候有错误, 仔细看日志发现需要缺少 Microsoft Visual C++ Compiler for Python 2.7 下载安装后,重新安装jupyter即可 htt ...