1. 程式人生 > >dijkstra+堆優化

dijkstra+堆優化

fine vector pty priority %d bit con cst for

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+堆優化