dijkstra.java 3.6 KB
Newer Older
qq_36480062's avatar
c  
qq_36480062 已提交
1 2
package graph;

qq_36480062's avatar
c  
qq_36480062 已提交
3
import java.io.*;
qq_36480062's avatar
c  
qq_36480062 已提交
4 5
import java.util.Arrays;
import java.util.PriorityQueue;
qq_36480062's avatar
c  
qq_36480062 已提交
6 7 8
import java.util.StringTokenizer;

import static java.lang.System.in;
qq_36480062's avatar
c  
qq_36480062 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

/**
 * 给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值。
 * 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1。
 * 输入格式
 * 第一行包含整数n和m。
 * 接下来m行每行包含三个整数x,y,z,表示点x和点y之间存在一条有向边,边长为z。
 * 输出一个整数,表示1号点到n号点的最短距离。
 * 如果路径不存在,则输出-1。
 * 数据范围
 * 1≤n,m≤10^5,
 * 图中涉及边长均不超过10000。
 * 输入样例:
 * 3 3
 * 1 2 2
 * 2 3 1
 * 1 3 4
 * 输出样例:
 * 3
 */
public class dijkstra {
    static class node implements Comparable<node> {
        int to, dis;

        public node(int to, int dis) {
            this.to = to;
            this.dis = dis;
        }

        @Override
        public int compareTo(node node) {
            return dis - node.dis;
        }

    }


qq_36480062's avatar
c  
qq_36480062 已提交
46
    public static void main(String[] args) throws IOException {
qq_36480062's avatar
qq_36480062 已提交
47
        //   Scanner sc = new Scanner(System.in);
qq_36480062's avatar
c  
qq_36480062 已提交
48 49 50
        n = nextInt();
        m = nextInt();
        int s = nextInt();
qq_36480062's avatar
c  
qq_36480062 已提交
51 52
        int a, b, c;
        for (int i = 0; i < m; i++) {
qq_36480062's avatar
c  
qq_36480062 已提交
53 54 55
            a = nextInt();
            b = nextInt();
            c = nextInt();
qq_36480062's avatar
c  
qq_36480062 已提交
56 57
            add(a, b, c);
        }
qq_36480062's avatar
c  
qq_36480062 已提交
58
        Arrays.fill(dis, (1 << 31));
qq_36480062's avatar
c  
qq_36480062 已提交
59
        dij(s);
qq_36480062's avatar
c  
qq_36480062 已提交
60 61
    }

qq_36480062's avatar
c  
qq_36480062 已提交
62 63 64
    private static void dij(int s) throws IOException {
        q.add(new node(s, 0));
        dis[s] = 0;
qq_36480062's avatar
c  
qq_36480062 已提交
65
        while (!q.isEmpty()) {
qq_36480062's avatar
qq_36480062 已提交
66
            int p = q.poll().to;
qq_36480062's avatar
c  
qq_36480062 已提交
67
            //pq每次取出的边,就是算出最短路径的边
qq_36480062's avatar
qq_36480062 已提交
68 69 70
            if (vis[p]) continue;
            vis[p] = true;
            for (int i = he[p]; i != 0; i = ne[i]) {
qq_36480062's avatar
c  
qq_36480062 已提交
71
                int ed = e[i];
qq_36480062's avatar
qq_36480062 已提交
72 73
                if (dis[p] != Integer.MAX_VALUE && dis[ed] > dis[p] + w[i]) {
                    dis[ed] = dis[p] + w[i];
qq_36480062's avatar
c  
qq_36480062 已提交
74 75 76 77
                    q.add(new node(ed, dis[ed]));
                }
            }
        }
qq_36480062's avatar
c  
qq_36480062 已提交
78 79 80 81
        for (int i = 1; i <= n; i++) {
            bw.write(dis[i] + " ");
        }
        bw.flush();
qq_36480062's avatar
c  
qq_36480062 已提交
82 83 84 85 86 87 88 89 90
    }

    static void add(int a, int b, int c) {
        e[cnt] = b;
        w[cnt] = c;
        ne[cnt] = he[a];
        he[a] = cnt++;
    }

qq_36480062's avatar
c  
qq_36480062 已提交
91

qq_36480062's avatar
c  
qq_36480062 已提交
92
    static int n, m, cnt = 1;
qq_36480062's avatar
c  
qq_36480062 已提交
93 94 95 96
    static int[] e = new int[500005];
    static int[] he = new int[500005];
    static int[] ne = new int[500005];
    static int[] w = new int[500005];
qq_36480062's avatar
c  
qq_36480062 已提交
97
    static boolean[] vis = new boolean[100005];
qq_36480062's avatar
c  
qq_36480062 已提交
98 99
    static PriorityQueue<node> q = new PriorityQueue<node>();
    static int[] dis = new int[100005];
qq_36480062's avatar
c  
qq_36480062 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException {// 读取下一行字符串
        return reader.readLine();
    }

    static String next() throws IOException {// 读取下一个字符串
        while (!tokenizer.hasMoreTokens()) {
            //如果没有字符了,就是下一个,使用空格拆分,
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {// 读取下一个int型数值
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {// 读取下一个double型数值
        return Double.parseDouble(next());
    }
qq_36480062's avatar
c  
qq_36480062 已提交
123
}
qq_36480062's avatar
c  
qq_36480062 已提交
124