結果

問題 No.160 最短経路のうち辞書順最小
ユーザー GrenacheGrenache
提出日時 2016-06-06 22:22:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 351 ms / 5,000 ms
コード長 3,777 bytes
コンパイル時間 4,490 ms
コンパイル使用メモリ 80,184 KB
実行使用メモリ 48,416 KB
最終ジャッジ日時 2024-04-17 05:22:54
合計ジャッジ時間 8,319 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
37,164 KB
testcase_01 AC 55 ms
37,140 KB
testcase_02 AC 55 ms
36,816 KB
testcase_03 AC 55 ms
36,976 KB
testcase_04 AC 142 ms
41,108 KB
testcase_05 AC 155 ms
43,712 KB
testcase_06 AC 168 ms
43,904 KB
testcase_07 AC 123 ms
40,456 KB
testcase_08 AC 131 ms
39,772 KB
testcase_09 AC 102 ms
39,856 KB
testcase_10 AC 113 ms
40,128 KB
testcase_11 AC 125 ms
40,400 KB
testcase_12 AC 122 ms
39,780 KB
testcase_13 AC 110 ms
39,824 KB
testcase_14 AC 124 ms
40,288 KB
testcase_15 AC 123 ms
39,504 KB
testcase_16 AC 123 ms
40,156 KB
testcase_17 AC 125 ms
40,192 KB
testcase_18 AC 118 ms
40,328 KB
testcase_19 AC 108 ms
39,804 KB
testcase_20 AC 124 ms
39,872 KB
testcase_21 AC 121 ms
40,124 KB
testcase_22 AC 122 ms
39,768 KB
testcase_23 AC 128 ms
40,212 KB
testcase_24 AC 130 ms
40,320 KB
testcase_25 AC 126 ms
40,260 KB
testcase_26 AC 114 ms
39,964 KB
testcase_27 AC 80 ms
37,984 KB
testcase_28 AC 351 ms
48,416 KB
testcase_29 AC 64 ms
37,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder160 {

    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Printer pr = new Printer(System.out);

		int n = sc.nextInt();
		int m = sc.nextInt();
		int s = sc.nextInt();
		int g = sc.nextInt();

		Dijkstra di = new Dijkstra(n);
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			int c = sc.nextInt();
			di.addEdge(a, b, c);
			di.addEdge(b, a, c);
		}

//		pr.println(di.getShortestPath(g, s));

		int[] path = di.getPath(g, s);

		List<Integer> ret = new ArrayList<>();
		ret.add(s);
		while (path[s] != -1) {
			ret.add(path[s]);
			s = path[s];
		}

		for (int i = 0; i < ret.size(); i++) {
			pr.print(ret.get(i));
			if (i < ret.size() - 1) {
				pr.print(" ");
			}
		}
		pr.println();

		pr.close();
		sc.close();
    }

	private static class Dijkstra {
		long[] d;
		List<Edge>[] edges;
		PriorityQueue<Vertex> pq;
		int n;
		int s;
		int[] prev;

		final long INF = Long.MAX_VALUE;

		@SuppressWarnings("unchecked")
		Dijkstra(int n) {
			this.n = n;

			edges = new List[n];
			for (int ii = 0; ii < n; ii++) {
				edges[ii] = new ArrayList<Edge>();
			}

			s = - 1;
		}

		// i, j:0-indexed
		public void addEdge(int i, int j, int c) {
			edges[i].add(new Edge(i, j, c));
		}

		public long getShortestPath(int i, int j) {
			if (s != i) {
				d = new long[n];
				Arrays.fill(d, INF);
				d[i] = 0;
				prev = new int[n];
				Arrays.fill(prev, -1);
				pq = new PriorityQueue<Vertex>();
				pq.add(new Vertex(i, d[i]));

				while (!pq.isEmpty()) {
					Vertex u = pq.poll();
					if (d[u.me] < u.d) {
						continue;  // skip old vertex
					}

					for (int ii = 0; ii < edges[u.me].size(); ii++) {
						Edge v = edges[u.me].get(ii);
						if (d[u.me] != INF && d[v.v] > d[u.me] + v.w) {
							d[v.v] = d[u.me] + v.w;
							pq.add(new Vertex(v.v, d[v.v]));
							prev[v.v] = u.me;
						} else if (d[u.me] != INF && d[v.v] == d[u.me] + v.w && prev[v.v] > u.me) {
							pq.add(new Vertex(v.v, d[v.v]));
							prev[v.v] = u.me;
						}
					}
				}

				s = i;
			}

			return d[j];
		}

		public int[] getPath(int i, int j) {
			if (s != i) {
				getShortestPath(i, j);
			}

			return prev;
		}

		private static class Edge {
//			int u; // from
			int v; // to
			int w; // cost

			Edge(int u, int v, int w) {
//				this.u = u;
				this.v = v;
				this.w = w;
			}
		}

		private static class Vertex implements Comparable<Vertex> {
			int me; // me
			long d; // cost

			Vertex(int u, long w) {
				this.me = u;
				this.d = w;
			}

			@Override
			public int compareTo(Vertex o) {
				return Long.compare(this.d, o.d);
//				return this.d > o.d ? 1 : this.d < o.d ? -1 : 0;
			}
		}
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

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

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

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

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

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0