#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. for循环的执行循序

    先上一段代码,大家说出此方法的执行结果: public class Print{ static boolean out(char c){ System.out.println(c); return t ...

  2. 部署网站时的错误“one of its dependencies.试图加载格式不正确的程序。”解决方案。

    Sever Error in '/' Application.Could not load file or assembly 'SresBase' or one of its dependencies ...

  3. TP系统常量信息

    [系统常量信息] 获取系统常量信息: 如果加参数true,会分组显示: 显示如下: [跨控制器调用] 一个控制器在执行的时候,可以实例化另外一个控制,并通过对象访问其指定方法. 跨控制器调用可以节省我 ...

  4. CodeIgniter框架——创建一个简单的Web站点(include MySQL基本操作)

    目标 使用 CodeIgniter 创建一个简单的 Web 站点.该站点将有一个主页,显示一些宣传文本和一个表单,该表单将发布到数据库表中. 按照 CodeIgniter 的术语,可将这些需求转换为以 ...

  5. filter和find区别,元素遍历

    转 filter和find区别 find()会在当前指定元素中查找符合条件的子元素,是对它的子集操作,而filter()则是在当前指定的元素集合中查找符合条件的元素,是对自身集合元素进行筛选. HTM ...

  6. HDU3037 附Lucas简单整理

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. 获取html元素所在页面的坐标

    function findPosition(oElement) { var x2 = 0; var y2 = 0; var width = oElement.offsetWidth; var heig ...

  8. 巨蟒python全栈开发linux之centos4

    1.linux虚拟环境1-4 2.linux运行crm代码

  9. Delphi运算符及优先级

    单目运算符 (最高优先级) @ 取变量或函数的地址(返回一个指针) not 逻辑取反或按位取反 乘除及按位运算符 * 相乘或集合交集 / 浮点相除 div 整数相除 mod 取模 (整数相除的余数) ...

  10. SOE 部署错误 ClassFactory cannot supply requested class

    问题描述: 部署完SOE,对某个服务启用部署的SOE时,出现错误信息,假如对地图服务SampleWorldCities启用刚部署的SOE,错误信息如下: service failed to start ...