結果

問題 No.3393 Move on Highway
コンテスト
ユーザー ks2m
提出日時 2025-11-28 23:08:03
言語 Java
(openjdk 23)
結果
AC  
実行時間 2,096 ms / 3,000 ms
コード長 2,681 bytes
コンパイル時間 2,905 ms
コンパイル使用メモリ 82,248 KB
実行使用メモリ 100,040 KB
最終ジャッジ日時 2025-11-28 23:08:53
合計ジャッジ時間 47,956 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		int c = Integer.parseInt(sa[2]);
		List<List<Hen>> list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			int u = Integer.parseInt(sa[0]) - 1;
			int v = Integer.parseInt(sa[1]) - 1;
			int w = Integer.parseInt(sa[2]);

			list.get(u).add(new Hen(v, w));
			list.get(v).add(new Hen(u, w));
		}
		br.close();

		long[] d1 = new long[n];
		Arrays.fill(d1, Long.MAX_VALUE);
		d1[0] = 0;
		PriorityQueue<Node> que = new PriorityQueue<Node>();
		que.add(new Node(0, 0, 0));
		while (!que.isEmpty()) {
			Node cur = que.poll();
			if (cur.d > d1[cur.v]) {
				continue;
			}
			for (Hen hen : list.get(cur.v)) {
				long alt = d1[cur.v] + hen.c + c;
				if (alt < d1[hen.v]) {
					d1[hen.v] = alt;
					que.add(new Node(hen.v, 0, alt));
				}
			}
		}

		long[] dn0 = new long[n];
		long[] dn1 = new long[n];
		Arrays.fill(dn0, Long.MAX_VALUE);
		Arrays.fill(dn1, Long.MAX_VALUE);
		dn0[n - 1] = 0;
		dn1[n - 1] = 0;
		que.clear();
		que.add(new Node(n - 1, 0, 0));
		while (!que.isEmpty()) {
			Node cur = que.poll();
			if (cur.v2 == 0 && cur.d > dn0[cur.v]) {
				continue;
			}
			if (cur.v2 == 1 && cur.d > dn1[cur.v]) {
				continue;
			}
			for (Hen hen : list.get(cur.v)) {
				long alt = dn0[cur.v] + hen.c + c;
				if (alt < dn0[hen.v]) {
					dn0[hen.v] = alt;
					que.add(new Node(hen.v, 0, alt));
				}

				alt = dn1[cur.v] + hen.c + c;
				if (alt < dn1[hen.v]) {
					dn1[hen.v] = alt;
					que.add(new Node(hen.v, 1, alt));
				}

				alt = dn0[cur.v] + c;
				if (alt < dn1[hen.v]) {
					dn1[hen.v] = alt;
					que.add(new Node(hen.v, 1, alt));
				}
			}
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 1; i < n; i++) {
			long ans = d1[i] + dn1[i];
			ans = Math.min(ans, d1[n - 1]);
			pw.println(ans);
		}
		pw.flush();
	}

	static class Hen {
		int v, c;

		public Hen(int v, int c) {
			this.v = v;
			this.c = c;
		}
	}

	static class Node implements Comparable<Node> {
		int v, v2;
		long d;

		public Node(int v, int v2, long d) {
			this.v = v;
			this.v2 = v2;
			this.d = d;
		}

		public int compareTo(Node o) {
			return Long.compare(d, o.d);
		}
	}
}
0