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));
            }
        }
    }
}

 

posted @ 2017-02-03 16:51  超级学渣渣  阅读(1354)  评论(0编辑  收藏  举报