问题 A: 最短路径迪杰斯特拉算法入门

题目描述

如图,求最短路径。
在这里插入图片描述

输入

顶点数n 边数m
m条边的顶点和权值
某两个顶点

输出

顶点0到每个顶点的最短路径

样例输入

6 9
0 2 5
0 3 30
1 0 2
1 4 8
2 1 15
2 5 7
4 3 4
5 3 10
5 4 18
0 4

样例输出

28
#include<bits/stdc++.h>
#define read() freopen("input.txt","r",stdin)
#define write() freopen("output.txt","w",stdout)
using namespace std;
const int maxn = 505;
const int INF = 0x3f3f3f;
struct Edge{
	int from,to,dist;
	Edge(int u,int v,int d):from(u),to(v),dist(d){};
};
struct HeapNode{
	int d,u;
	bool operator < (const HeapNode & rhs)const{
		return d>rhs.d;
	}
};
struct Dijkstra{
	int n,m;
	vector<Edge>edges;
	vector<int>G[maxn];
	bool done[maxn];
	int d[maxn];
	int p[maxn];
	void init(int n){
		this->n=n;
		for( int i=0; i<n; i++ ) G[i].clear();
		edges.clear();
	}
	void AddEdge(int from,int to,int dist){
		edges.push_back(Edge(from,to,dist));
		m=edges.size();
		G[from].push_back(m-1);
	}
	void dijkstra(int s){
		priority_queue<HeapNode>Q;
		for( int i=0; i<n; i++ ) d[i]=INF;
		d[s]=0;
		memset(done,0,sizeof(done));
		Q.push((HeapNode){0,s});
		while(!Q.empty()){
			HeapNode x = Q.top();Q.pop();
			int u = x.u;
			if(done[u]) continue;
			done[u]=true;
			for( int i=0; i<G[u].size(); i++ ){
				Edge& e=edges[G[u][i]];
				if(d[e.to]>d[u]+e.dist){
					d[e.to]=d[u]+e.dist;
					p[e.to]=G[u][i];
					Q.push((HeapNode){d[e.to],e.to});
				}
			}
		}
	}
};
int main(){
	read();
	int n,m,s,d;
	cin>>n>>m;
	Dijkstra temp;
	temp.init(n);
	for( int i=0; i<m; i++ ){
		int u,v,d;cin>>u>>v>>d;
		temp.AddEdge(u,v,d);
	}
	cin>>s>>d;
	temp.dijkstra(s);
	cout<<temp.d[d];
	return 0;
}

Java代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.Vector;

public class Main {
    private static final int SIZE = 505;
    private static final int INF = 0x3f3f3f;

    static class Edge {
        int from, to, dist;
        Edge(int f, int t, int d) {
            from = f;
            to = t;
            dist = d;
        }
    }

    static class HeapNode {
        int dist, from;
        HeapNode(int d, int f) {
            dist = d;
            from = f;
        }
    }

    static class Dijkstra {
        int n, m;
        Vector<Edge> edges = new Vector<>();
        Vector<Integer> G[] = new Vector[SIZE];
        boolean done[] = new boolean[SIZE];
        int d[] = new int[SIZE];
        int p[] = new int[SIZE];

        void init(int num) {
            n = num;
            for(int i=0; i<SIZE; i++) G[i] = new Vector<>();
        }

        void addEdge(int from, int to, int dist) {
            edges.add(new Edge(from, to, dist));
            int m = edges.size();
            G[from].add(m - 1);
        }

        void dijkstra(int s) {
            PriorityQueue<HeapNode> queue = new PriorityQueue<>(new Comparator<HeapNode>() {
                @Override
                public int compare(HeapNode o1, HeapNode o2) {
                    if(o1.dist < o2.dist) return 1;
                    else return -1;
                }
            });
            for(int i=0; i<n; i++) {
                d[i] = INF;
                done[i] = false;
            }
            d[s] = 0;
            queue.add(new HeapNode(0, s));
            while (!queue.isEmpty()) {
                HeapNode topNode = queue.poll();
                int node = topNode.from;
                if(done[node] == true) continue;
                done[node] = true;
                for(int i=0; i<G[node].size(); i++) {
                    Edge tmpEdge = edges.get(G[node].get(i));
                    if(d[tmpEdge.to] > d[node] + tmpEdge.dist) {
                        d[tmpEdge.to] = d[node] + tmpEdge.dist;
                        p[tmpEdge.to] = G[node].get(i);
                        queue.add(new HeapNode(d[tmpEdge.to], tmpEdge.to));
                    }
                }
            }
        }
    }
    public static void main(String[] args) throws IOException {
        Reader reader = new Reader(System.in);
        int n, m, s, d;
        n = reader.nextInt();
        m = reader.nextInt();
        Dijkstra dijkstra = new Dijkstra();
        dijkstra.init(n);
        for(int i=0; i<m; i++) {
            int from, to, dist;
            from = Reader.nextInt();
            to = Reader.nextInt();
            dist = Reader.nextInt();
            dijkstra.addEdge(from, to, dist);
        }
        s = reader.nextInt();
        d = reader.nextInt();
        dijkstra.dijkstra(s);
        System.out.println(dijkstra.d[d]);
    }

    static class Reader {
        static BufferedReader reader;
        static StringTokenizer tokenizer;

        public Reader(InputStream input) {
            reader = new BufferedReader(new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        static String next() throws IOException {
            while (!tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        static long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        static int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        static double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值