HDU 2544 用优先队列写的最短路径
2014-10-09 15:06
453 查看
#include <iostream> #include <queue> #include <vector> #include <cstdio> #include <cstring> #define INF 0x7fffffff using namespace std; struct edge { int s,t,len; }; vector <edge> v; vector <int> p[110]; int dis[110]; int n,m; void add(int s,int t,int l) { v.push_back((edge) { s,t,l }); v.push_back((edge) { t,s,l }); int number=v.size(); p[s].push_back(number-2); p[t].push_back(number-1); } struct node { int pos; int l; friend bool operator <(node a,node b) { return a.l>b.l; } }; priority_queue<node> q; bool vis[110]; void sho() { memset(vis,0,sizeof(vis)); dis[1]=0; for(int i=2; i<=n; i++) dis[i]=INF; q.push((node) { 1,0 }); while(!q.empty()) { node st=q.top(); q.pop(); vis[st.pos]=1; for(int i=0; i<(int)p[st.pos].size(); i++) { edge& kk=v[p[st.pos][i]]; if(!vis[kk.t] && dis[kk.t]>dis[st.pos]+kk.len) { dis[kk.t]=dis[st.pos]+kk.len; q.push((node) { kk.t,dis[kk.t] }); } } } } int main() { while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; v.clear(); for(int i=1; i<=n; i++) p[i].clear(); int s,t,l; for(int i=0; i<m; i++) { scanf("%d%d%d 4000 ",&s,&t,&l); add(s,t,l); } sho(); printf("%d\n",dis ); } return 0; }
相关文章推荐
- (step6.2.3)hdu 2544(最短路——最短路径)
- hdu 1026 Ignatius and the Princess I (bfs记录路径 两种记录路径方法模拟优先队列和优先队列两种做法)
- [最短路径] HDU 2544 - 最短路
- hdu2544——最短路(最短路径模板)
- 广度优先搜索 最短路径 队列
- hdu 2544最短路径
- hdu 2544 最短路 (最短路径)
- zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】
- hdu2544——最短路径
- java dijkstra 单源 最短路径 优先队列 n趟
- poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra
- 最短路径——优先队列优化版(其实稠密图不优化)
- HDU 2544最短路径问题
- 迪杰斯特拉算法 最短路径模板题 hdu 2544
- hdu-2544-最短路(迪杰斯特拉-最短路径)
- Dijkstra单源最短路径算法; 优先队列+静态数组邻接表; STL优先队列还是没想明白排序原则;
- hdu 1026 Ignatius and the Princess I bfs 优先队列 路径记录
- hdu 1026 优先队列 + 路径保存
- 所有节点对最短路径 超时 优先队列 + dijkstra + 遍历前驱子图
- 【HDU 1874 2544 2066 2112】 Dijkstra单源最短路径专题 —— 优先队列+邻接表/邻接矩阵