dijkstra队列优化链式前向星(C++、Java)

C++

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int maxm = 4010;  //边
const int maxn = 1010; //点
const int inf = 0x3f3f3f3f;

struct Edge
{
    int to,v,next;
} edge[maxm];
struct node
{
    int num, val; //存编号 和距离
    node(int _num=0, int _val=0):num(_num), val(_val) {}
    bool operator <(const node &tmp) const
    {
        return val > tmp.val;
    }
};
int head[maxn];
int top;
int n,m;
int dis[maxn];
bool vis[maxn];

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

void addedge(int from, int to, int v)
{
    edge[top].to = to;
    edge[top].v = v;
    edge[top].next = head[from];
    head[from] = top++;
}
void dijkstra(int st) //起点
{
    priority_queue que;
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    dis[st] = 0;
    que.push(node(st, 0));
    while(!que.empty())
    {
        node p = que.top();
        que.pop();
        int nown = p.num;
        if(vis[nown])
            continue;
        vis[nown] = true;
        for(int i=head[nown]; i!=-1; i=edge[i].next)
        {
            Edge e = edge[i];
            if(dis[e.to] > dis[nown] + e.v && !vis[e.to])
            {
                dis[e.to] = dis[nown] + e.v;
                que.push(node(e.to,dis[e.to]));
            }
        }
    }
}
int main()
{

    return 0;
}

Java

import java.util.*;
class Edge
{
    public int to;
    public int next;
    public double v;
    public Edge(int _to, int _next, double _v){
        this.to = _to;
        this.next = _next;
        this.v = _v;
    }
}
class node
{
    public int num;
    public double val;
    public node(int _num, double _val){
        this.num = _num;
        this.val = _val;
    }
}
public class dijk {
    private int cnt = 0;
    private double[] dis;
    private boolean[] vis;
    private int[] head;
    private Edge[] edge;
    private Queue pq;
    private Comparator Order;

    public dijk(int _n, int _m) //n个点 m条边
    {
        dis = new double[_n+1];
        vis = new boolean[_n+1];
        head = new int[_n+1];
        edge = new Edge[_m+1];
        Arrays.fill(head,-1);
        Arrays.fill(dis, 0x3f);
        Arrays.fill(vis, false);
        Order = new Comparator() {
            public int compare(node a, node b) {
                if(a.val > b.val)
                    return 1;
                else if(a.val > b.val)
                    return -1;
                else
                    return 0;
            }
        };
        pq = new PriorityQueue(Order);
    }
    public void addEdge(int from, int to, double v){
        edge[cnt] = new Edge(to,head[from],v);
        head[from] = cnt++;
    }
    public double cal(int st,int ed){
        dis[st] = 0;
        pq.add(new node(st,0));
        while(!pq.isEmpty()){
            node p = pq.poll();
            int nown = p.num;
            if(vis[nown])
                continue;
            vis[nown] = true;
            for(int i=head[nown]; i!=-1; i=edge[i].next){
                Edge e = edge[i];
                if(dis[e.to] > dis[nown] + e.v && !vis[e.to]){
                    dis[e.to] = dis[nown] + e.v;
                    pq.add(new node(e.to,dis[e.to]));
                }
            }
        }
        return dis[ed];
    }
}

你可能感兴趣的:(图论/数论)