Shortest Path Algorithm (Standard)

40 篇文章 0 订阅

35. Shortest Path Algorithm (Standard)

 #include<bits/stdc++.h>
 using namespace std;
 #define maxn 200010
 #define inf 0x3fffffff
 int n,m,s;
 int v[maxn];
 long long ans[maxn];
 bool vis[maxn];
 typedef struct
 {
     int index;
     int w;
     int next; 
 }en[maxn];
 en e;
 typedef struct node
 {
     long long u,ws;
     bool operator<(const node &x) const
     {
         return  x.ws<ws;
     }
 }node;
 ​
 void createg()
 {
     cin>>n>>m>>s;
     int r,c,ww;
     for(int i=1;i<=m;i++)
     {
         cin>>r>>c>>ww;
         e[i].index=c;
         e[i].w=ww;
         e[i].next=v[r];
         v[r]=i;
     }
 }
 void djistra()
 {
     for(int i=1;i<=n;i++)
     {
         ans[i]=inf;
     }
     ans[s]=0;
     priority_queue<node> Q;
     Q.push((node){s,0});
     while(!Q.empty())
     {
         node fr = Q.top();Q.pop();
         int s = fr.u;
         if(vis[s]) continue;
         vis[s]=true;
         for(int i=v[s];i!=0;i=e[i].next)
         {
             if(ans[e[i].index]>ans[s]+e[i].w)
             {
                 ans[e[i].index]=ans[s]+e[i].w;
                 if(!vis[e[i].index])
                 Q.push((node){e[i].index,ans[e[i].index]});
             }
         }
     }
 }
 void printsg()
 {
     for(int i=1;i<n;i++)
     {
         printf("%lld ",ans[i]);
     }
     printf("%lld",ans[n]);
 }
 int main()
 {
     createg();
     djistra();
     printsg();
     return 0;
 }

1. Dijkstra + Priority_queue

 

1. About priority_queue

  • #include<queue>

  • definition

     
    typedef struct node
     {
         long long u,ws;
         bool operator>(const node &x) const
         {
             return  x.ws<ws;
         }
     }node;
     priority_queue<node> Q;

     

  • priority to input greater data

2. Question

Question Link

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值