优先队列解决最短路问题
来源:互联网 发布:三星手机数据更新 编辑:程序博客网 时间:2024/06/15 23:50
用优先队列解决最短路问题,会将复杂度降低
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#define inf 999999using namespace std;struct pp{ int pos; int cost; pp(){} pp(int xx, int yy):pos(xx), cost(yy){} bool operator < (const pp &a) const{ return cost > a.cost; }};struct qq{ int to; int val; qq(){} qq(int xx, int yy):to(xx), val(yy){}};priority_queue<pp>mm;vector<qq>edge[1100];int n, m, d[1010];bool vis[1010];int main(){ int i; while(scanf("%d%d", &m, &n)!=EOF){ for(i = 1; i <= n; i++) edge[i].clear(); while(!mm.empty()){ mm.pop(); } memset(vis, 0, sizeof(vis)); for(i = 1; i <= n; i++) d[i] = inf; int a, b, c; for(i = 1; i <= m; i++){ scanf("%d%d%d", &a, &b, &c); edge[a].push_back(qq(b, c)); edge[b].push_back(qq(a, c)); } d[1] = 0; mm.push(pp(1, 0)); while(!mm.empty()){ pp node = mm.top(); mm.pop(); if(!vis[node.pos]){ for(i = 0; i < edge[node.pos].size(); i++){ if(d[edge[node.pos][i].to] > d[node.pos] + edge[node.pos][i].val){ d[edge[node.pos][i].to] = d[node.pos] + edge[node.pos][i].val; mm.push(pp(edge[node.pos][i].to, d[edge[node.pos][i].to])); } } vis[node.pos] = 1; } } printf("%d\n", d[n]); } return 0;}
0 0
- 优先队列解决最短路问题
- 广度优先遍历解决最短路问题
- 优先队列Dijkstra实现最短路算法
- poj 3662 最短路+优先队列+二分法
- 最短路(Dijkstra+优先队列)
- poj3635-DP+优先队列搜索最短路
- 最短路算法(Dijsktra + 优先队列)
- ZOJ 2526(最短路+优先队列)
- POJ 2312<优先队列><最短路>
- 最短路-邻接表(优先队列)写
- hdu 2112 最短路 dijkstra优先队列
- hdu2544 最短路(dijkstra/优先队列)
- 最短路&&优先队列优化->HDU2544
- HDU5521 双向最短路+优先队列优化
- HDU1242(优先队列,最短路)Rescue
- 最短路算法(Dijsktra + 优先队列)
- dijkstra + 优先队列 最短路 [sdut 2143]
- 最短路的优先队列的优化
- 学到的工作方法,望助你一臂之力
- web前端性能优化–缓存
- HDU 1540 Tunnel Warfare(线段树)
- 学习笔记 static
- 长连接和短连接性能测试结果差异
- 优先队列解决最短路问题
- android ContentResolver详解
- hdu 5047 大数找规律
- C++知识点:static的作用
- Spring + RabbitMQ 配置
- 【阅读】《Head First jQuery》——前言、第一章(编写jquery的准备活动和一个jquery移动元素的小例子)
- JS中typeof()的用法
- JBoss AS 6.1.0 Final Kernel 启动过程类的关系
- vi/vim 删除:一行, 一个字符, 单词, 每行第一个字符 命令