1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#include <cstdio> #include <cstring> #include <queue> #include <utility> using
namespace std;
const
int N=20005;
const
int INF=9999999;
typedef
pair< int , int >seg;
priority_queue<seg,vector,greater >q; int
d[N],head[N],u[N],v[N],w[N],next[N],n,m,a,b,c;
bool
vis[N];
void
build(){
memset (head,-1, sizeof (head));
for ( int
e=1;e<=m;e++){
scanf ( "%d%d%d" ,&u[e],&v[e],&w[e]);
u[e+m]=v[e]; v[e+m]=u[e]; w[e+m]=w[e];
next[e]=head[u[e]]; head[u[e]]=e;
next[e+m]=head[u[e+m]]; head[u[e+m]]=e+m;
}
} void
Dijkstra( int
src){
memset (vis,0, sizeof (vis));
for ( int
i=0;i<=n;i++) d[i]=INF;
d[src]=0;
q.push(make_pair(d[src],src));
while (!q.empty()){
seg now=q.top(); q.pop();
int
x=now.second;
if (vis[x]) continue ; vis[x]= true ;
for ( int
e=head[x];e!=-1;e=next[e])
if (d[v[e]]>d[x]+w[e]){
d[v[e]]=d[x]+w[e];
q.push(make_pair(d[v[e]],v[e]));
}
}
} int
main(){
while (~ scanf ( "%d%d" ,&n,&m)&&n+m){build();Dijkstra(1); printf ( "%d\n" ,d[n]);}
return
0;
} |
相关文章
- 11-04C++优先队列例子
- 11-041102. 得分最高的路径 C++ 优先队列
- 11-04使用C++优先队列(priority_queue)解决Top K问题
- 11-04C++模板学习之优先队列实现
- 11-04C++优先队列使用
- 11-04C++ - 库函数优先级队列(priority_queue)输出最小值 代码
- 11-04C++模板:Dijkstra+优先队列
- 11-04【C++ 语言】容器 ( queue 队列 | stack 栈 | priority_queue 优先级队列 | set 集合 | 容器遍历 | map )(一)
- 11-04C++ 优先队列(priority_queue)用法
- 11-04用C++的类做三种优先队列的实现