• Dijkstra堆优化


    #include<iostream>
    #include<cstdio>
    #include<queue>
    
    using namespace std;
    
    int dis[200001],head[200001];
    int n,m,a,b,c,s,cnt;
    bool vis[200001];
    
    struct edge
    {
        int to;
        int dis;
        int next;
    };
    
    edge e[500001];
    
    struct node
    {
        int dis;
        int pos;
        bool operator<(const node &x)const
        {
            return x.dis<dis;
        }
    };
    
    void add_edge(int u,int v,int d)
    {
        cnt++;
        e[cnt].to=v;
        e[cnt].dis=d;
        e[cnt].next=head[u];
        head[u]=cnt;
    }
    
    std::priority_queue <node> q;
    
    void dijkstra()
    {
        dis[s]=0;
        q.push((node){0,s});
        while(!q.empty())
        {
            node tmp=q.top();
            q.pop();
            int x=tmp.pos,d=tmp.dis;
            if(vis[x])
                continue;
            vis[x]=1;
            for(int i=head[x];i;i=e[i].next)
            {
                int y=e[i].to;
                if(dis[y]>dis[x]+e[i].dis)
                {
                    dis[y]=dis[x]+e[i].dis;
                    if(!vis[y])
                        q.push((node){dis[y],y});
                }
            }
                           }    
    }
    
    int main()
    {
        scanf("%d%d%d",&n,&m,&s);
        fill(dis+1,dis+n+1,0x7fffffff);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
            //add_edge(b,a,c);
        }
        dijkstra();
        for(int i=1;i<=n;i++)
            printf("%d ",dis[i]);
        return 0;
    }
  • 相关阅读:
    哇~~
    震作震作~~life is only Soso
    真j8无聊
    川行之旅
    可爱的java,我又来看你了,
    暑假周进度总结报告2
    暑假周进度总结报告1
    暑假周进度总结报告3
    2017级软工经验交流体验
    暑假周进度总结报告4
  • 原文地址:https://www.cnblogs.com/liumengliang/p/13184300.html
Copyright © 2020-2023  润新知