結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ぴろずぴろず
提出日時 2015-02-26 02:16:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 633 ms / 5,000 ms
コード長 3,178 bytes
コンパイル時間 4,204 ms
コンパイル使用メモリ 84,540 KB
実行使用メモリ 63,980 KB
最終ジャッジ日時 2023-09-06 04:54:01
合計ジャッジ時間 15,412 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 173 ms
56,788 KB
testcase_01 AC 172 ms
56,984 KB
testcase_02 AC 173 ms
56,972 KB
testcase_03 AC 183 ms
57,176 KB
testcase_04 AC 354 ms
61,716 KB
testcase_05 AC 411 ms
60,608 KB
testcase_06 AC 478 ms
60,920 KB
testcase_07 AC 305 ms
61,352 KB
testcase_08 AC 319 ms
61,672 KB
testcase_09 AC 299 ms
61,248 KB
testcase_10 AC 312 ms
61,356 KB
testcase_11 AC 329 ms
61,156 KB
testcase_12 AC 306 ms
61,880 KB
testcase_13 AC 300 ms
61,440 KB
testcase_14 AC 305 ms
61,272 KB
testcase_15 AC 302 ms
61,748 KB
testcase_16 AC 301 ms
61,352 KB
testcase_17 AC 314 ms
61,328 KB
testcase_18 AC 309 ms
61,540 KB
testcase_19 AC 306 ms
61,336 KB
testcase_20 AC 311 ms
63,408 KB
testcase_21 AC 304 ms
61,796 KB
testcase_22 AC 304 ms
61,504 KB
testcase_23 AC 336 ms
61,740 KB
testcase_24 AC 324 ms
61,512 KB
testcase_25 AC 319 ms
61,668 KB
testcase_26 AC 296 ms
60,904 KB
testcase_27 AC 255 ms
60,668 KB
testcase_28 AC 633 ms
63,980 KB
testcase_29 AC 235 ms
58,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package graph.ans2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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.addEdge(a[i], b[i], c[i], (char) b[i]);
			g.addEdge(b[i], a[i], c[i], (char) a[i]);
		}

		return g.shortestPathDijkstra(start, goal); //コストが特殊なダイクストラ
	}

}
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 addEdge(int from,int to,int cost,char c) {
		graph[from].add(new Edge(to, cost, c));
	}

	//dijkstra O(ElogV)
	public ArrayList<Integer> shortestPathDijkstra(int s,int g) {
		Dist[] dist = new Dist[n];
		int[] prev = new int[n]; //経路復元用
		Arrays.fill(dist, Dist.INF);
		dist[s] = Dist.ZERO;
		PriorityQueue<Node> q = new PriorityQueue<Node>();
		q.offer(new Node(Dist.ZERO, s));
		while(!q.isEmpty()) {
			Node node = q.poll();
			int v = node.id;
			if (dist[v].compareTo(node.dist) < 0) {
				continue;
			}
			for(Edge e:graph[v]) {
				Dist d = new Dist(dist[v].dist + e.cost, dist[v].path + e.c);
				if (dist[e.to].compareTo(d) > 0) {
					prev[e.to] = v;
					dist[e.to] = d;
					q.add(new Node(dist[e.to], e.to));
				}
			}
		}

		ArrayList<Integer> path = new ArrayList<>();
		int now = g;
		while(now != s) {
			path.add(now);
			now = prev[now];
		}
		path.add(s);
		Collections.reverse(path);

		return path;
	}

	static class Edge {
		int to;
		int cost;
		char c;
		public Edge(int to,int cost,char c) {
			this.to = to;
			this.cost = cost;
			this.c = c;
		}
	}
	static class Node implements Comparable<Node>{
		Dist dist;
		int id;
		public Node(Dist dist,int i) {
			this.dist = dist;
			this.id = i;
		}
		public int compareTo(Node o) {
			return dist.compareTo(o.dist);
		}
	}
	static class Dist implements Comparable<Dist>{
		static Dist ZERO = new Dist(0,"");
		static Dist INF = new Dist(Graph.INF,"[");
		int dist;
		String path;
		public Dist(int dist,String path) {
			this.dist = dist;
			this.path = path;
		}
		public int compareTo(Dist o) {
			int c1 = Integer.compare(dist, o.dist);
			if (c1 != 0) {
				return c1;
			}
			return path.compareTo(o.path);
		}

	}
}
0