POJ 3159 最短路 SPFA
#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的更多相关文章
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
- POJ 1511 最短路spfa
题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...
- poj 3013 最短路SPFA算法
POJ_3013_最短路 Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23630 ...
- It's not a Bug, It'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 ...
- POJ 3159 Candies(spfa、差分约束)
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
随机推荐
- ios 关于collectionView.performBatchUpdates()方法 --时时更新
今天想实现一个简单的collectionView动画效果,查阅相关资料发现,实现 collectionView.performBatchUpdates()方法即可,于是掉坑里了. 文档: public ...
- FFmpeg总结(六)AV系列结构体之AVPacket
AVPacket位置:libavcodec/avcodec.h下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVqanVubGlu/font/5a6 ...
- [Spring MVC]学习笔记--基础Servlet
Servlet是一个用Java编写的应用程序,在服务器上运行,处理请求的信息并将其发送到客户端. Servlet的客户端提出请求并获得该请求的响应. 对于所有的客户端请求,只需要创建Servlet的实 ...
- Cocos2d-x Lua中使用标签
游戏场景中的文字包括了静态文字和动态文字.静态文字如下图所示游戏场景中①号文字“COCOS2DX”,动态文字如图4-1所示游戏场景中的②号文字“Hello World”.静态文字一般是由美工使用Pho ...
- 爬虫实战【6】Ajax内容解析-今日头条图集
Ajax技术 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). Ajax并不是新的编程语言,而是一种使用现有标准的新方法,当然 ...
- Online Judge(字符串-格式)
Online Judge Problem Description Ignatius is building an Online Judge, now he has worked out all the ...
- Introduction to Mathematical Thinking - Week 7
Q: Why did nineteenth century mathematicians devote time to the proof of self-evident results? Selec ...
- 通过margin负值去除padding
.pay-type { // 图片布局前通过margin负值去除padding margin: 0 -@page-padding-horizontal; display: inline-flex; } ...
- ThreadPoolExecutor 线程池任务队列分析 与 利特尔法则(Little's law)
一. 演示 public class ThreadPoolTest { static class MyThread implements Runnable { private String name; ...
- mui 视频播放
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...