hdu 2544 最短路
来源:互联网 发布:阿里云网站日志在哪里 编辑:程序博客网 时间:2024/06/11 00:50
最短路的入门题
朴素的dij写了一遍之后拿优先队列又写了一遍
#include <iostream>#include <cstdio>#include <map>#include <queue>#include <vector>using namespace std;struct edge{ int to, dist; edge(int v, int d) : to(v), dist(d) {}};struct node{ int d, from; node(int dd, int too): d(dd), from(too) {} bool operator < (const node& rhs) const{ return d > rhs.d; }};const int maxn = 1e6;const int inf = 0x3f3f3f3f;vector <edge> edges[maxn];int d[maxn];int n;void dijkstra(int s){ priority_queue <node> q; for (int i = 0; i != n; ++i) d[i] = inf; d[s] = 0; q.push(node(0,s)); while (!q.empty()) { node x = q.top(); q.pop(); if (x.d != d[x.from]) //若该点已出现过则跳过 类似与朴素的dij中的vis数组。 continue; for (int i = 0; i != edges[x.from].size(); ++i) { edge& e = edges[x.from][i]; //对从from出发的各条边进行松弛操作 if (d[e.to] > d[x.from] + e.dist) { d[e.to] = d[x.from] + e.dist; q.push(node(d[e.to], e.to)); } } }}int main(){ int k; while (cin >> n >> k && n) { int p1, p2, dis; for (int i = 0; i != k; ++i) //无向图 建立双向边 { cin >> p1 >> p2 >> dis; edges[p1-1].push_back(edge(p2-1,dis)); edges[p2-1].push_back(edge(p1-1,dis)); } dijkstra(0); if (d[n-1] == inf) cout << "-1" << endl; else cout << d[n-1] << endl; for (int i = 0; i != 2*k; ++i) edges[i].clear(); } return 0;}
0 0
- 最短路 2544hdu
- HDU 2544 最短路
- HDU-2544 最短路
- Hdu-2544 最短路
- HDU 2544 最短路
- hdu 2544 最短路
- HDU 2544 ( 最短路 )
- hdu 2544 最短路
- HDU 2544 最短路
- hdu 2544 最短路
- hdu 2544 最短路
- HDU 2544 最短路
- hdu 2544 最短路
- HDU 2544 最短路
- hdu 2544 最短路
- HDU 2544 最短路
- hdu 2544 最短路
- hdu 2544 最短路
- OC中字典
- redis源码阅读[2]
- vs2010项目属性配置
- poj1201(贪心+树状数组)
- OC中字符串NSstring
- hdu 2544 最短路
- IDEA JVM参数
- uva12716 GCD XOR(打表找规律+筛法)
- QT 绘制 五角图案
- 自适应图像分辨率
- Moore's voting algorithm
- Swift学习笔记之数组(三)
- Cmake使用教程
- 递归函数时间复杂度分析