結果

問題 No.160 最短経路のうち辞書順最小
ユーザー GrenacheGrenache
提出日時 2016-06-06 22:22:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 272 ms / 5,000 ms
コード長 3,777 bytes
コンパイル時間 4,017 ms
コンパイル使用メモリ 80,260 KB
実行使用メモリ 58,892 KB
最終ジャッジ日時 2024-10-08 16:46:02
合計ジャッジ時間 8,471 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
50,448 KB
testcase_01 AC 50 ms
50,364 KB
testcase_02 AC 52 ms
50,024 KB
testcase_03 AC 51 ms
50,436 KB
testcase_04 AC 128 ms
54,800 KB
testcase_05 AC 150 ms
55,596 KB
testcase_06 AC 158 ms
55,704 KB
testcase_07 AC 107 ms
52,144 KB
testcase_08 AC 122 ms
52,568 KB
testcase_09 AC 102 ms
52,232 KB
testcase_10 AC 117 ms
52,464 KB
testcase_11 AC 127 ms
52,816 KB
testcase_12 AC 121 ms
52,484 KB
testcase_13 AC 114 ms
52,204 KB
testcase_14 AC 118 ms
52,568 KB
testcase_15 AC 101 ms
52,268 KB
testcase_16 AC 110 ms
52,508 KB
testcase_17 AC 113 ms
52,512 KB
testcase_18 AC 113 ms
52,412 KB
testcase_19 AC 110 ms
52,064 KB
testcase_20 AC 115 ms
52,688 KB
testcase_21 AC 110 ms
52,404 KB
testcase_22 AC 105 ms
52,584 KB
testcase_23 AC 121 ms
52,688 KB
testcase_24 AC 119 ms
52,420 KB
testcase_25 AC 113 ms
52,400 KB
testcase_26 AC 115 ms
52,360 KB
testcase_27 AC 63 ms
50,796 KB
testcase_28 AC 272 ms
58,892 KB
testcase_29 AC 55 ms
50,332 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