package graph.ans2a; //http://yukicoder.me/submissions/14000 の無駄を省いた版 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 ans = solve(n, m, s, g, a, b, c); StringBuilder sb = new StringBuilder(); for(int i=0;i 0) { sb.append(' '); } sb.append(ans.get(i)); } System.out.println(sb.toString()); } public static ArrayList 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 ans = new ArrayList<>(); ans.add(start); for(int i=0;i[] graph; @SuppressWarnings("unchecked") public Graph(int n) { this.n = n; this.graph = new ArrayList[n]; for(int i=0;i(); } } 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 Dist[] dijkstra(int s) { Dist[] dist = new Dist[n]; Arrays.fill(dist, Dist.INF); dist[s] = Dist.ZERO; PriorityQueue q = new PriorityQueue(); 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 + (char) (e.to)); if (dist[e.to].compareTo(d) > 0) { dist[e.to] = d; q.add(new Node(dist[e.to], e.to)); } } } return dist; } static class Edge { int to; int cost; public Edge(int to,int cost) { this.to = to; this.cost = cost; } } static class Node implements Comparable{ 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{ static Dist ZERO = new Dist(0,""); static Dist INF = new Dist(Graph.INF,"["); int dist; String path; //経路を無理やりStringとして保存する 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); } } }