zoukankan      html  css  js  c++  java
  • dijkstra模板

    
    #include<cstdio>
    #include<queue>
    using namespace std;
    #define pa pair<int,int>
    #define maxn 100010
    priority_queue<pa,vector<pa>,greater<pa> > q;
    struct edge
    {
        int val,to;
    };
    int n,m,s,dis[maxn];
    bool vis[maxn];
    vector<edge>e[maxn]; 
    int main()
    {
        scanf("%d%d%d",&n,&m,&s);
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            edge tmp;
            tmp.to=y;
            tmp.val=z;
            e[x].push_back(tmp);
        }
        for(int i=1;i<=n;i++)
        {
            dis[i]=2147483647;
        }
        dis[s]=0;
        q.push(make_pair(0,s));
        while(q.empty()==0)
        {
            int x=q.top().second;
            q.pop();
            if(vis[x]==1)
            continue;
            vis[x]=1;
            for(int i=0;i<e[x].size();i++)
            {
                int y=e[x][i].to;
                if(dis[x]+e[x][i].val<dis[y])
                {
                    dis[y]=dis[x]+e[x][i].val;
                    q.push(make_pair(dis[y],y));
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            printf("%d ",dis[i]);
        }
        return 0;
    }
    
  • 相关阅读:
    会场安排
    Comet OJ
    CodeForces1154F
    CodeForces1154E
    2019.08.25校内模拟赛Graph
    2019.08.25校内模拟赛Page
    [MtOI2019]灵梦的计算器
    [MtOI2019]永夜的报应
    [NOI2018]归程
    USACO[CowCoupons]
  • 原文地址:https://www.cnblogs.com/ShineEternal/p/11220633.html
Copyright © 2011-2022 走看看