使用优先队列+邻接表的Dijkstra算法
来源:互联网 发布:建筑软件开发 编辑:程序博客网 时间:2024/05/12 18:16
Description
Tanvir returned home from the contest and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest? After checking a bit he found that there is no brush in him room. So, he called Atiq to get a brush. But as usual Atiq refused to come. So, Tanvir decided to go to Atiq's house.
The city they live in is divided by some junctions. The junctions are connected by two way roads. They live in different junctions. And they can go to one junction to other by using the roads only.
Now you are given the map of the city and the distances of the roads. You have to find the minimum distance Tanvir has to travel to reach Atiq's house.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a blank line. The next line contains two integers N (2 ≤ N ≤ 100) and M (0 ≤ M ≤ 1000), means that there are N junctions and M two way roads. Each of the next M lines will contain three integers u v w (1 ≤ u, v ≤ N, w ≤ 1000), it means that there is a road between junction u and v and the distance is w. You can assume that Tanvir lives in the 1st junction and Atiq lives in the Nth junction. There can be multiple roads between same pair of junctions.
Output
For each case print the case number and the minimum distance Tanvir has to travel to reach Atiq's house. If it's impossible, then print 'Impossible'.
Sample Input
2
3 2
1 2 50
2 3 10
3 1
1 2 40
Sample Output
Case 1: 60
Case 2: Impossible
#include<stdio.h>#include<string>#include<stdlib.h>#include<utility>#include<cstring>#include<iostream>#include<queue>#define INF 1000005#define MAXN 2010using namespace std;typedef pair<int,int>pii;//捆绑编号跟dis[i]的值int v[MAXN];int w[MAXN];int next[MAXN];int head[MAXN];int dis[110];int n,m;void Dijkstra(){priority_queue<pii,vector<pii>,greater<pii> >q;//最小值优先出队for(int i=1; i<=n; i++) dis[i]=INF;dis[1]=0;q.push(make_pair(dis[1], 1));while(!q.empty()){pii u=q.top();q.pop();int x=u.second;if(u.first!=dis[x]) continue;for(int i=head[x]; i!=-1; i=next[i]){if(dis[x] + w[i] < dis[v[i]]){dis[v[i]] = dis[x] + w[i];q.push(make_pair(dis[v[i]], v[i]));}}}}int main(){#if 0freopen("in.txt","r",stdin);#endifint T,t=0;scanf("%d", &T);while(T--){int size=0;printf("Case %d: ", ++t);memset(head,-1,sizeof(head));scanf("%d%d", &n,&m);while(m--){int a,b,c;scanf("%d%d%d", &a,&b,&c);v[size]=b;w[size]=c;next[size]=head[a];head[a]=size++;v[size]=a;//因为是无向图,故两边都要弄w[size]=c;next[size]=head[b];head[b]=size++;}Dijkstra();if(dis[n]!=INF) printf("%d\n", dis[n]);else printf("Impossible\n");}return 0;}
- 使用优先队列+邻接表的Dijkstra算法
- Dijkstra(邻接表+优先队列)
- Dijkstra算法实现类—提高,邻接表+优先队列
- ACM:最短路,dijkstra,邻接表的建立,使用邻接表跟优先队列的dijkstra,Bellman-Ford,Floyd。。
- 使用优先队列的Dijkstra算法
- Dijkstra + 优先队列 + 邻接表优化
- 图算法 单源最短路径 Dijkstra算法(邻接表/邻接矩阵+优先队列STL)
- (转)图算法单源最短路径Dijkstra算法(邻接表/邻接矩阵+优先队列STL)
- 基于邻接表和优先级队列的Dijkstra算法实现
- Dijkstra单源最短路径算法; 优先队列+静态数组邻接表; STL优先队列还是没想明白排序原则;
- [最短路]使用优先队列优化的Dijkstra算法
- Sicily 1031 Campus(Dijkstra[邻接表+优先队列优化])
- Sicily 1031 Campus(Dijkstra[邻接表+优先队列优化])
- HDU 2544 最短路 静态邻接表+优先队列 dijkstra
- Dijkstra[两种邻接表+优先队列优化]
- 【POJ1511】Invitation Cards 【dijkstra+优先队列+邻接表】
- hdu 2544 最短路(Dijkstra 邻接表+优先队列)
- 优先队列优化的Dijkstra算法
- 设计模式观后(c++还原之二十三 状态模式)
- Microsoft SQL Server Management Studio cannot find one or more components的问题
- 支付宝SDK IOS
- 常用开源协议-转自 小胡子哥的Blong
- Gridview的footer模板中放置控件实现添加功能
- 使用优先队列+邻接表的Dijkstra算法
- 闹钟便签功能
- jQuery设置聚焦并使光标位置在文字最后
- Hibernate一些常用注解
- python初接触
- 南邮数据结构实验1.2 单链表的操作
- 连续子数组的最大和:线性扫描和分治法
- 第一篇博客
- mongodb 脚本多集合关联查询更新