ALGO-5 最短路

ALGO-5 最短路

题目

资源限制

时间限制:1.0s 内存限制:256.0MB

问题描述

给定一个 n 个顶点,m 条边的有向图(其中某些边权可能为负,但保证没有负环)。请你计算从 1 号点到其他点的最短路(顶点从 1 到 n 编号)。

输入格式

第一行两个整数 n, m。

接下来的 m 行,每行有三个整数 u, v, l,表示 u 到 v 有一条长度为 l 的边。

输出格式

共 n-1 行,第 i 行表示 1 号点到 i+1 号点的最短路。

样例输入

3 3
1 2 -1
2 3 -1
3 1 2

样例输出

-1
-2

数据规模与约定

对于 10%的数据,n = 2,m = 2。
对于 30%的数据,n <= 5,m <= 10。
对于 100%的数据,1 <= n <= 20000,1 <= m <= 200000,-10000 <= l <= 10000,保证从任意顶点都能到达其他所有顶点。

题解

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;

public class ALGO_5 {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    public static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    /**
    @description: record the information of edge
     */
    static class Edge {
        int end;
        int length;

        public Edge(int end, int length) {
            this.end = end;
            this.length = length;
        }
    }

    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int n = nextInt();
        int m = nextInt();
        ArrayList<ArrayList<Edge>> edge = new ArrayList<ArrayList<Edge>>();
        for (int i = 0; i < n; i++)
            edge.add(new ArrayList<Edge>());
        for (int i = 0; i < m; i++) {
            int u = nextInt();
            int v = nextInt();
            int l = nextInt();
            edge.get(u - 1).add(new Edge(v - 1, l));
        }
        int[] length = BellmanFord(edge, n);
        for (int i = 1; i < length.length; i++) {
            out.println(length[i]);
        }
        out.flush();
    }

    /**
    @description: the shortest path length from the first to others
    @param {ArrayList<ArrayList<Edge>>} edge
    @param {int} n
    @return {int[]} spl(shortest path length)
     */
    public static int[] BellmanFord(ArrayList<ArrayList<Edge>> edge, int n) {
        // TODOlength is the final array, initialize it with inf(MAX_VALUE).
        int[] spl = new int[n];
        for (int i = 0; i < spl.length; i++)
            spl[i] = Integer.MAX_VALUE;
        // TODOjudge show if Edge is passed.
        boolean[] judge = new boolean[n];
        // TODOpath is record the possible Edge
        Queue<Integer> path = new ArrayDeque<Integer>();
        path.add(0); // init path, add the start Edge
        while (!path.isEmpty()) {
            int start = path.poll();
            judge[start] = false;
            // pass through all edge from the node
            for (int i = 0; i < edge.get(start).size(); i++) {
                int end = edge.get(start).get(i).end;
                int length = edge.get(start).get(i).length;
                // find the shortest path length
                if (spl[end] > spl[start] + length) {
                    spl[end] = spl[start] + length;
                    if (!judge[end]) {
                        path.add(end);
                        judge[end] = true;
                    }
                }
            }
        }
        return spl;
    }

}

posted @ 2022-03-18 13:22  morning-start  阅读(25)  评论(0编辑  收藏  举报