#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. MPEG 编解码相关资料收集

    以下是我搜集的关于MPEG1/2的编解码相关的资料: (注:mpge帧内编码是基于jpeg编码的,所以请务必先理解jpeg的编解码原理.) 1:Introduction to MPEG 2 Video ...

  2. Chromium开发--入门C++小样例

      開始做Chromium开发的时候,都是从代码去摸索的,如今有几个样例了.希望看到的同学能有帮助,比直接在大量代码里领悟要快非常多了. C++ in Chromium 101 - Codelab h ...

  3. log4j日志文件乱码问题的解决方法

    近日在AIX上用log4j打印日志,出现乱码,经过努力解决问题. 症状:在默认语言非中文(或者说默认语言不支持中文的)的Windows.Linux.Unix上,用log4j打印日志,出现乱码,常见的就 ...

  4. Java工程师面试题整理[社招篇]

    http://blog.csdn.net/jackfrued/article/details/44921941 1.面向对象的特征有哪些方面?2.访问修饰符public,private,protect ...

  5. 统计TCP网络连接情况

    #!/bin/bash metric=$1 tmp_file=/tmp/tcp_status.txt /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a i ...

  6. KVC && KVO 初见

    Look,这是一个很简单的要求,点击Add me,age +1. 想一想的话很简单的,设置一个属性Nsinteger age,点击button add me,直接加1在重新显示Lable就好啦,不过, ...

  7. git commit -a -m "M 1、引入mixin,公共样式mixin传参处理;";git push origin master:master

    <script> import wepy from 'wepy' import api from '../api/api' export default class recharge ex ...

  8. 人工智能-baidu-aip语音合成(文字转语音)

    from aip import AipSpeech APP_ID = ' APP_KEY = 'DhXGtWHYMujMVZZGRI3a7rzb' SECRET_KEY = 'PbyUvTL31fIm ...

  9. nodejs获取服务器数据到页面

    const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const route ...

  10. Oracle学习笔记—数据库,实例,表空间,用户、表之间的关系

    之前一直使用的关系型数据库是Mysql,而新公司使用Oracle,所以最近从网上搜集了一些资料,整理到这里,如果有不对的地方,欢迎大家讨论. 基本概念: 数据库:Oracle 数据库是数据的物理存储. ...