結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ぴろずぴろず
提出日時 2015-02-26 02:16:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 458 ms / 5,000 ms
コード長 2,908 bytes
コンパイル時間 3,018 ms
コンパイル使用メモリ 77,052 KB
実行使用メモリ 62,236 KB
最終ジャッジ日時 2023-09-06 04:52:43
合計ジャッジ時間 11,199 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,540 KB
testcase_01 AC 125 ms
54,300 KB
testcase_02 AC 122 ms
56,164 KB
testcase_03 AC 132 ms
55,776 KB
testcase_04 AC 261 ms
60,052 KB
testcase_05 AC 319 ms
62,236 KB
testcase_06 AC 374 ms
60,868 KB
testcase_07 AC 223 ms
59,352 KB
testcase_08 AC 231 ms
59,368 KB
testcase_09 AC 223 ms
59,104 KB
testcase_10 AC 240 ms
59,528 KB
testcase_11 AC 239 ms
59,632 KB
testcase_12 AC 230 ms
59,288 KB
testcase_13 AC 221 ms
59,436 KB
testcase_14 AC 228 ms
59,552 KB
testcase_15 AC 220 ms
59,416 KB
testcase_16 AC 225 ms
59,452 KB
testcase_17 AC 224 ms
59,636 KB
testcase_18 AC 215 ms
59,764 KB
testcase_19 AC 228 ms
59,268 KB
testcase_20 AC 230 ms
59,644 KB
testcase_21 AC 217 ms
59,364 KB
testcase_22 AC 212 ms
59,552 KB
testcase_23 AC 236 ms
59,804 KB
testcase_24 AC 238 ms
59,708 KB
testcase_25 AC 225 ms
59,424 KB
testcase_26 AC 223 ms
59,500 KB
testcase_27 AC 190 ms
56,460 KB
testcase_28 AC 458 ms
60,616 KB
testcase_29 AC 181 ms
55,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package graph.ans1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int s = sc.nextInt();
		int g = sc.nextInt();
		int[] a = new int[m];
		int[] b = new int[m];
		int[] c = new int[m];
		for(int i=0;i<m;i++) {
			a[i] = sc.nextInt();
			b[i] = sc.nextInt();
			c[i] = sc.nextInt();
		}
		ArrayList<Integer> ans = solve(n, m, s, g, a, b, c);
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<ans.size();i++) {
			if (i > 0) {
				sb.append(' ');
			}
			sb.append(ans.get(i));
		}
		System.out.println(sb.toString());
	}

	public static ArrayList<Integer> solve(int n,int m,int start,int goal,int[] a,int[] b,int[] c) {
		Graph g = new Graph(n);
		for(int i=0;i<m;i++) {
			g.addBidirectionalEdge(a[i], b[i], c[i]); //無向辺の追加(有向辺を2つ張る)
		}

		int[] dist = g.minDistDijkstra(goal); //ゴールからダイクストラ

		//辞書順最小になるように、経路復元しながら貪欲的に選ぶ
		//無向辺なので実装は簡単
		ArrayList<Integer> ans = new ArrayList<>();
		int now = start;
		ans.add(now);
		while(true) {
			int next = 1 << 29;
			for(Graph.Edge e:g.graph[now]) { //nowから出てる辺全てに対して処理
				if (dist[now] == dist[e.to] + e.cost) {
					next = Math.min(next, e.to);
				}
			}
			now = next;
			ans.add(now);
			if (now == goal) {
				break;
			}
		}
		return ans;
	}

}
class Graph {
	public static final int INF = 1<<29;
	int n;
	ArrayList<Edge>[] graph;

	@SuppressWarnings("unchecked")
	public Graph(int n) {
		this.n = n;
		this.graph = new ArrayList[n];
		for(int i=0;i<n;i++) {
			graph[i] = new ArrayList<Edge>();
		}
	}

	public void addBidirectionalEdge(int from,int to,int cost) {
		addEdge(from,to,cost);
		addEdge(to,from,cost);
	}
	public void addEdge(int from,int to,int cost) {
		graph[from].add(new Edge(to, cost));
	}

	//dijkstra O(ElogV)
	public int[] minDistDijkstra(int s) {
		int[] dist = new int[n];
		Arrays.fill(dist, INF);
		dist[s] = 0;
		PriorityQueue<Node> q = new PriorityQueue<Node>();
		q.offer(new Node(0, s));
		while(!q.isEmpty()) {
			Node node = q.poll();
			int v = node.id;
			if (dist[v] < node.dist) {
				continue;
			}
			for(Edge e:graph[v]) {
				if (dist[e.to] > dist[v] + e.cost) {
					dist[e.to] = dist[v] + e.cost;
					q.add(new Node(dist[e.to], e.to));
				}
			}
		}
		return dist;
	}

	class Edge {
		int to;
		int cost;
		public Edge(int to,int cost) {
			this.to = to;
			this.cost = cost;
		}
	}
	class Node implements Comparable<Node>{
		int dist;
		int id;
		public Node(int dist,int i) {
			this.dist = dist;
			this.id = i;
		}
		public int compareTo(Node o) {
			return (this.dist < o.dist) ? -1 : ((this.dist == o.dist) ? 0 : 1);
		}
	}
}
0