zoukankan      html  css  js  c++  java
  • 堆优化的dijkstra

    https://www.luogu.org/problem/show?pid=1339

    #include<cstdio>
    #include<queue>
    #include<cstring>
    #define N 55000
    #define M 1000000
    using namespace std;
    int n,m,s,t,tot;
    int nxt[M],to[M],front[N],cap[M];
    int DIS[N];
    bool vis[N];
    struct edge
    {
        int number,dis;
        bool operator <(edge b) const
        {
            return dis>b.dis;
        }
    }now,nt;
    priority_queue<edge>q;
    void add(int u,int v,int w)
    {
        to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; cap[tot]=w;
        to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; cap[tot]=w;
    }
    bool dijkstra()
    {
        for(int i=1;i<=n;i++) DIS[i]=2e9;
        q.push((edge){s,0});
        DIS[s]=0;
        while(!q.empty())
        {
            now=q.top();q.pop();
            if(vis[now.number]) continue;
            vis[now.number]=true;
            for(int i=front[now.number];i;i=nxt[i])
            {
                if(DIS[to[i]]>DIS[now.number]+cap[i])
                {
                    DIS[to[i]]=DIS[now.number]+cap[i];
                    q.push((edge){to[i],DIS[to[i]]});
                }
            }
        }
        printf("%d",DIS[t]);
    }
    
    int main()
    {
        int u,v,w;
        scanf("%d%d%d%d",&n,&m,&s,&t);
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        dijkstra();
    }
  • 相关阅读:
    XML组成部分
    XML语法
    XML概念
    HTTP协议:响应消息的数据格式---Response
    KM HDU 3718
    KM最大匹配 HDU 2255
    匈牙利算法
    母函数
    最长公共子序列 LCS
    hdu 4632 区间DP
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6504422.html
Copyright © 2011-2022 走看看