2013寒假ACM集训_最短路
2013-02-27 11:21
525 查看
一、单源最短路
1、正权
dijkstra
2、任意权
2、1 bellman-ford
2、2 spfa
二、任意点之间最短路
floyd
三、最短路的应用:差分约束系统
参考模板
http://www.toposort.com/blog/shortest-path.html
贴一个新的最新的 spfa模板:
1、正权
dijkstra
2、任意权
2、1 bellman-ford
2、2 spfa
二、任意点之间最短路
floyd
三、最短路的应用:差分约束系统
参考模板
http://www.toposort.com/blog/shortest-path.html
贴一个新的最新的 spfa模板:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <queue> #include <algorithm> #include <vector> #define LL __int64 const int maxm = 2e6 + 10; const int maxn = 5e5 + 10; const int INF = 1<<28; using namespace std; int cnt, head[maxn], d[maxn], vis[maxn], n, m; struct node { int v, w, next; }edge[2*maxm]; void add(int u, int v, int w) { edge[cnt].v = v; edge[cnt].w = w; edge[cnt].next = head[u]; head[u] = cnt++; }; void spfa(int s) { queue<int>q; int i, u, v; for(i = 0; i <= n; i++) d[i] = INF; d[s] = 0; q.push(s); vis[s] = 1; while(!q.empty()) { u = q.front(); q.pop(); vis[u] = 0; for(i = head[u]; i != -1; i = edge[i].next) { v=edge[i].v; if(d[u] + edge[i].w < d[v]) { d[v] = d[u] + edge[i].w; if(!vis[v]) { vis[v] = 1; q.push(v); } } } } }; int main() { int i; int a, b, w; int s, t; while(~scanf("%d%d", &n, &m)) { cnt = 0; memset(head, -1, sizeof(head)); memset(vis, 0, sizeof(vis)); for(i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &w); add(a, b, w); add(b, a, w); } scanf("%d%d", &s, &t); spfa(s); printf("%d\n", d[t]); } }
相关文章推荐
- ACM寒假集训部分题目总结
- 水题啊水题,可我偏偏没做出来!!!(SDUT 2013春季ACM集训_12级周赛3 A题 )
- ACM暑期集训——专题二[最短路Bellman-Ford算法]
- 【2013寒假SD高校ACM周赛4(SDJZU & SDUT)】 超级玛丽
- 【读题坑爹】 2013寒假SD高校ACM周赛总结
- 【打表找规律】2013寒假SD高校ACM周赛5——I’ve Got Your Back(gammon)
- ACM暑假集训第二章——最短路
- HLJU14级寒假集训之最短路专场
- (寒假集训) Piggyback(最短路)
- 2013.3.6 SDUT 2013春季ACM集训_12级周赛2 A题 ZOJ 2965
- 山东理工大学 2017级寒假ACM集训阶段测试赛题解
- (寒假集训)Roadblock(最短路)
- 2014寒假ACM集训13级PK赛-翻转排序
- ACM暑期集训——专题一[最短路Dijkstra算法]
- ACMer菜鸟的ACM寒假集训感悟
- 2014寒假ACM集训13级PK赛-购买矿石
- 2014寒假ACM集训13级PK赛-最佳拟合直线
- SDUT_2015寒假集训_最短路_A-最短路(Floyd)
- 2017级寒假ACM集训结训赛--官方题解
- ACM暑期集训——专题二[最短路Floyd算法]