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条路,每条路都有相应的承重能力,然后让你 ...
随机推荐
- Rightscale & Amazon
原先一直以为Rightscale是Amazno旗下的一个产品,今天才知道是Amazon partner - -||,实在汗颜. Rightscale也是一个很强大的公司,提供跨云解决方案...(呃,原 ...
- 解决java.lang.NoClassDefFoundError错误
昨天在开发过程中,导入同事的一个服务到本地,首先从git上把项目拉下来,然后使用maven导入eclipse,然后build. build的过程中在项目的一个测试类里面报错 java.lang.NoC ...
- [Spring Framework]学习笔记--Dependency injection(DI)
1. 通过构造函数实现DI 简单类型实例 package examples; public class ExampleBean { // Number of years to calculate th ...
- c#文件流汇总
操作文件比较常见,项目中经常出现这样的需求:按每个月自动创建文件,并且向文件里面插入一些数据,那么我们将要分析,文件是否存在的情况:如果存在则直接打开文件流向文件中插入数据,如果不存在,则创建文件再插 ...
- jQuery实现鼠标放到图片上,放大图片
<script src="../../Script/jquery-1.7.2.js" type="text/javascript"></scr ...
- Token bucket
w https://en.wikipedia.org/wiki/Token_bucket
- 如何定义 match 常量?
namespace MathConstants { const double E = 2.71828182845904523536; // e const double LOG2E = 1.44269 ...
- 常用模块一(random模块、time模块、sys模块)
一.random模块 import random # 1 取随机小数 应用:数学计算 ret = random.random() # 大于0且小于1之间的小数 print(ret) # 0.53559 ...
- HDFS涉及ACLs的命令
What is ACL Hadoop中的ACL与Linux中的ACL机制基本相同,都是用于为文件系统提供更精细化的权限控制. 参考 HDFS ACLs: Fine-Grained Permission ...
- windows下python调用c文件流程
1.新建fun.c文件和fun.h文件 #include <stdio.h> #include <stdlib.h> #include <string.h> int ...