zoukankan      html  css  js  c++  java
  • 最短路 Dijkstra+堆优化

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<vector>
    using namespace std;
    int n,m,s,t,cnt,head[100001];
    int dis[100001],vis[100001];
    struct uio{
        int to,nxt,wei;
    }edge[10000001];
    struct cmp{
        bool operator () (int x,int y)
        {return dis[x]>dis[y];};
    };
    priority_queue<int,vector<int>,cmp> que;
    void add(int x,int y,int z)
    {
        edge[++cnt].nxt=head[x];
        edge[cnt].to=y;
        edge[cnt].wei=z;
        head[x]=cnt;
    }
    void dijkstra()
    {
        memset(dis,0x3f,sizeof(dis));
        dis[s]=0;
        que.push(s);
        while(!que.empty())
        {
            int x=que.top();
            que.pop();
            if(!vis[x])
            {
                vis[x]=1;
                for(int i=head[x];i;i=edge[i].nxt)
                {
                    int y=edge[i].to;
                    dis[y]=min(dis[y],dis[x]+edge[i].wei);
                    que.push(y);
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d%d%d",&n,&m,&s,&t);
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        dijkstra();
        return 0;
    }
  • 相关阅读:
    一道题DP
    BZOJ 3155: Preprefix sum
    BZOJ:3209: 花神的数论题
    TJU 4087. box
    BZOJ1192: [HNOI2006]鬼谷子的钱袋
    概率DP
    Codeforces Round #253 (Div. 2) D题
    二维树状数组
    Codeforces Round #250 (Div. 2)
    莫比乌斯函数
  • 原文地址:https://www.cnblogs.com/water-radish/p/9280682.html
Copyright © 2011-2022 走看看