码迷,mamicode.com
首页 > 其他好文 > 详细

dijkstra+堆优化

时间:2019-05-14 19:30:10      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:ios   struct   vector   printf   scanf   class   long   span   empty   

http://acm.hdu.edu.cn/showproblem.php?pid=2544

 1 #include<bits/stdc++.h>
 2 #include<vector>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<cstdio>
 6 #define ll long long
 7 using namespace std;
 8 struct note
 9 {
10     int v,p;
11     note() {}
12     note(int a,int b):v(a),p(b) {}
13 
14 };
15 struct cmp
16 {
17     operator ()(note a,note b)
18     {
19         return a.p>b.p;
20     }
21 
22 };
23 int jl[103];
24 int vim[103];
25 vector<note>q[103];
26 priority_queue<note,vector<note>,cmp>pp;
27 void df(int stae)
28 {
29     memset(jl,0x3f3f3f3f,sizeof(jl));
30     memset(vim,0,sizeof(vim));
31     pp.push(note(stae,0));
32     jl[stae]=0;
33     while(!pp.empty())
34     {
35         note now=pp.top();
36         pp.pop();
37         if(vim[now.v])continue;
38         vim[now.v]=1;
39         for(int i=0; i<q[now.v].size(); i++)
40         {
41             int v=q[now.v][i].v;
42             int p=q[now.v][i].p;
43             if(!vim[v]&&jl[v]>jl[now.v]+p)
44             {
45                 jl[v]=jl[now.v]+p;
46                 pp.push(note(v,jl[v]));
47             }
48         }
49     }
50 }
51 int n,m;
52 int main()
53 {
54 
55     while(scanf("%d%d",&n,&m)&&n+m)
56     {
57         memset(ss,0x3f3f3f3f,sizeof(ss));
58         for(int i=1; i<=n; i++)q[i].clear();
59         for(int i=1; i<=m; i++)
60         {
61             int a,b,c;
62             scanf("%d%d%d",&a,&b,&c);
63             q[a].push_back(note(b,c));
64             q[b].push_back(note(a,c));
65         }
66         df(1);
67         printf("%d\n",jl[n]);
68     }
69 }

 

dijkstra+堆优化

标签:ios   struct   vector   printf   scanf   class   long   span   empty   

原文地址:https://www.cnblogs.com/zxz666/p/10863755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!