zoukankan      html  css  js  c++  java
  • Dijkstra普通算法及优先队列优化

    #include<stdio.h>
    #include<iostream>
    #define maxv 100
    #define inf 0x3fffffff
    using namespace std;
    
    int cost[maxv][maxv];
    int d[maxv];
    bool used[maxv];
    int V;
    
    void dijkstra(int s)
    {
        for(int i=0;i<v;i++) d[i]=inf;
        d[s]=0;
        fill(used,used+v,false);
    
        while(true)
        {
            int v=-1;
            for(int u=0;u<V;u++)
            {
                if(!used[u]&&(v==-1||d[u]<d[v])) v=u;
            }
            if(v=-1) break;
            used[v]=true;
            for(int u=0;u<V;u++)
            {
                if(d[u]>d[v]+cost[v][u])
                    d[u]=d[v]+cost[v][u]
            }
        }
    }
    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #define maxv 1000
    #define inf 0x3fffffff
    using namespace std;
    
    struct edge
    {
        int to;
        int cost;
    };
    
    typedef pair<int,int> P;//cost v
    int  V;
    vector<edge>G[maxv];
    int d[maxv];
    void difkstra(int s)
    {
        priority_queue <P,vector<P>,greater<P> >que;
        fill(d,d+V,inf);
        d[s]=0;
        que.push(P(0,s));
        while(!que.empty())
        {
            P p=que.top();que.pop();
            int v=p.second;
            for(int i=0;i<G[v].size();i++)
            {
                edage e=G[v][i];
                if(d[e.to]>d[v]+e.cost)
                {
                    d[e.to]=d[v]+e.cost;
                    que.push(P(d[e.to],e.to));
                }
            }
        }
    }
  • 相关阅读:
    bzoj1053(反素数)
    poj1442(对顶堆)
    poj2823(单调队列)
    poj3630(简单tire)
    poj1924(单调栈求最大矩阵)
    最大xor路径(poj3764)
    poj2689
    求n!末尾0的个数
    BigInteger和BigDecimal的基本用法
    大数乘法
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/6362975.html
Copyright © 2011-2022 走看看