SPFA算法模板
2015-10-09 18:59
399 查看
[code]#include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; const int size = 200010; int head[size],next[size],dist[size]; struct dc { int t,d; }l[size]; bool use[size]; int pri[size]; int t,c,s,e; int tot = 1; void build(int f,int t,int d) { l[tot].d = d; l[tot].t = t; next[tot] = head[f]; head[f] = tot ++; } queue < int > q; void spfa(int s) { dist[s] = 0; use[s] = 1; q.push(s); while(!q.empty()) { int f = q.front(); q.pop(); use[f] = 0; for(int i = head[f] ; i ; i = next[i]) { int t = l[i].t; if(dist[t] > dist[f] + l[i].d) { dist[t] = dist[f] + l[i].d; pri[t] = f; if(!use[t]) { use[t] = 1; q.push(t); } } } } } int main() { scanf("%d%d%d%d",&t,&c,&s,&e); for(int i = 1 ; i <= t ; i ++) dist[i] = 214748364; for(int i = 1 ; i <= c ; i ++) { int f,t,d; scanf("%d%d%d",&f,&t,&d); build(f,t,d); build(t,f,d); } spfa(s); printf("%d",dist[e]); return 0; }
例题codevs1557 , 两点间的最短路径
相关文章推荐
- isfinite() isinf() isnan() isnormal()
- 软考系列——排序算法盘点
- Java总结篇系列:Java多线程(二)
- SLIC超像素生成算法
- Fragment和Actiity的交互关系讲解(整理别人的)
- IOS开发:动画1
- 实验一报告
- XMPP 环境配置
- C++运算符
- (The one day)About Android 系统状态栏与APP主题颜色一致(方法二)
- SLIC超像素(superpixel)算法
- java split函数应该注意的问题
- 入侵方法
- 小知识
- node.js发送邮件email
- Android中联系人和通话记录详解(联系人的增删改查)(3)
- ASP注入
- ASP注入
- 如果看了此文你还不懂傅里叶变换,那就过来掐死我吧
- matplotlib文章