1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 using namespace std; 6 const int maxn1=2505; 7 const int maxn2=6205; 8 const int INF=0x3f3f3f3f; 9 int dist[maxn1],head[maxn1]; 10 bool vis[maxn1]; 11 int a,b,c,d; 12 struct pot{ 13 int id; 14 int goal; 15 bool operator <(const struct pot & AA)const 16 { 17 return goal > AA.goal; 18 } 19 }; 20 struct edge{ 21 int to; 22 int next; 23 int len; 24 25 }EDGE[maxn2*2]; 26 int edge_cnt=0; 27 priority_queue<struct pot>pq; 28 void add(int x,int y,int z) 29 { 30 EDGE[edge_cnt].to=y; 31 EDGE[edge_cnt].len=z; 32 EDGE[edge_cnt].next=head[x]; 33 head[x]=edge_cnt++; 34 } 35 void dij() 36 { 37 while(!pq.empty())pq.pop(); 38 pq.push((struct pot){c,0}); 39 while(!pq.empty()) 40 { 41 struct pot qwq=pq.top();pq.pop(); 42 if(dist[qwq.id]<qwq.goal) 43 continue; 44 if(qwq.id==d)break; 45 vis[qwq.id]=1; 46 for(int i = head[qwq.id];i!=-1;i=EDGE[i].next) 47 { 48 int v=EDGE[i].to; 49 if(qwq.goal+EDGE[i].len<dist[v]&&!vis[v]) 50 { 51 dist[v]=qwq.goal+EDGE[i].len; 52 pq.push((struct pot){v,dist[v]}); 53 } 54 } 55 } 56 57 } 58 int main() 59 { 60 scanf("%d%d%d%d",&a,&b,&c,&d); 61 for(int i = 1 ; i <= a ; i++) 62 { 63 dist[i]=INF; 64 } 65 dist[c]=0; 66 memset(head,-1,sizeof(head)); 67 memset(vis,0,sizeof(vis)); 68 while(b--) 69 { 70 int q,w,e; 71 scanf("%d%d%d",&q,&w,&e); 72 add(q,w,e); 73 add(w,q,e); 74 } 75 dij(); 76 printf("%d\n",dist[d]); 77 78 return 0; 79 }
堆优化的dij【模板】
猜你喜欢
转载自www.cnblogs.com/MekakuCityActor/p/8987593.html
今日推荐
周排行