hdu 2544 最短路
来源:互联网 发布:沈阳网络电玩城 编辑:程序博客网 时间:2024/06/05 09:00
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544
堆优化的dijkstra求最短路。。。
#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<map>using std::map;using std::cin;using std::cout;using std::endl;using std::find;using std::sort;using std::pair;using std::vector;using std::multimap;using std::priority_queue;#define pb(e) push_back(e)#define sz(c) (int)(c).size()#define mp(a, b) make_pair(a, b)#define all(c) (c).begin(), (c).end()#define iter(c) decltype((c).begin())#define cls(arr,val) memset(arr,val,sizeof(arr))#define cpresent(c, e) (find(all(c), (e)) != (c).end())#define rep(i, n) for (int i = 0; i < (int)(n); i++)#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)const int N = 11000;typedef unsigned long long ull;struct P { int w, v; P(int i = 0, int j = 0) :w(i), v(j) {} inline bool operator<(const P &a) const { return w > a.w; }};struct Node { int to, w, next; };int n, m;struct Dijkstra { Node G[N]; int tot, dist[N], head[N]; inline void init() { tot = 0; cls(head, -1), cls(dist, 0x3f); } inline void add_edge(int u, int v, int w) { G[tot] = { v, w, head[u] }; head[u] = tot++; } inline void built(int m) { int u, v, w; rep(i, m) { scanf("%d %d %d", &u, &v, &w); add_edge(u, v, w), add_edge(v, u, w); } } inline void dijkstra(int s) { dist[s] = 0; priority_queue<P> q; q.push(P(0, s)); while (!q.empty()) { P t = q.top(); q.pop(); int u = t.v; if (dist[u] < t.w) continue; for (int i = head[u]; ~i; i = G[i].next) { int &w = dist[G[i].to]; if (w > dist[u] + G[i].w) { w = dist[u] + G[i].w; q.push(P(w, G[i].to)); } } } printf("%d\n", dist[n]); }}go;int main() {#ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout);#endif while (~scanf("%d %d", &n, &m), n + m) { go.init(); go.built(m); go.dijkstra(1); } 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 最短路
- LeetCode题解——Generate Parentheses
- Unity脚本函数的执行顺序
- POJ1144
- 杂谈之单元测试的必要性
- dp cf B. Maximum Submatrix 2
- hdu 2544 最短路
- 机房收费系统之初体验
- 【Spark】RDD机制实现模型
- 网桥代理大师
- VB编程概述
- 大数开根号(蓝桥杯-矩阵翻硬币)
- 【Spark】Spark容错机制
- 静态 static
- C8_指针