首页 > 其他 > 详细

Dijkstra + 优先队列优化 模板

时间:2016-01-18 22:33:47      阅读:189      评论:0      收藏:0      [点我收藏+]
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;

const int MAXN = (int)(1e3 + 10);
const int MAXM = (int)(1e4 + 10);

typedef pair<int, int> pii;
struct cmp{
    bool operator () (pii a, pii b){
        return a.first > b.first;
    }
};

int size, head[MAXN], point[MAXM], next[MAXM], val[MAXM];
int dis[MAXN], t, n;

void init()
{
        size = 0;
        memset(head, -1, sizeof head);
}

void add(int from, int to, int value)//单向边
{
        val[size] = value;
    point[size] = to;
    next[size] = head[from];
    head[from] = size++;
}

void dijkstra(int s)
{
    memset(dis, 0x3f, sizeof dis);
    priority_queue<pii, vector<pii>, cmp> q;
    q.push(make_pair(0, s));
    dis[s] = 0;
    while(!q.empty()){
        pii u = q.top();
        q.pop();
        if(u.first > dis[u.second]) continue;
        for(int i = head[u.second]; ~i; i = next[i]){
            int j = point[i];
            if(dis[j] > u.first + val[i]){
                dis[j] = u.first + val[i];
                q.push(make_pair(dis[j], j));
            }
        }
    }
}

 

Dijkstra + 优先队列优化 模板

原文:http://www.cnblogs.com/quasar/p/5140575.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!