結果

問題 No.1190 Points
ユーザー tenten
提出日時 2020-08-24 22:39:30
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,680 ms / 2,000 ms
コード長 2,552 bytes
コンパイル時間 2,923 ms
コンパイル使用メモリ 89,180 KB
実行使用メモリ 87,772 KB
最終ジャッジ日時 2025-03-26 02:49:00
合計ジャッジ時間 36,622 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static final int MAX = Integer.MAX_VALUE / 2;
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
     	int m = sc.nextInt();
     	int pp = sc.nextInt();
    	int s = sc.nextInt() - 1;
     	int g = sc.nextInt() - 1;
      	ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
      	for (int i = 0; i < n; i++) {
      	    graph.add(new ArrayList<>());
      	}
      	for (int i = 0; i < m; i++) {
      	    int a = sc.nextInt() - 1;
      	    int b = sc.nextInt() - 1;
      	    graph.get(a).add(b);
      	    graph.get(b).add(a);
      	}
      	int[][] costsS = new int[2][n];
      	int[][] costsG = new int[2][n];
      	Arrays.fill(costsS[0], MAX);
      	Arrays.fill(costsS[1], MAX);
      	Arrays.fill(costsG[0], MAX);
      	Arrays.fill(costsG[1], MAX);
      	PriorityQueue<Path> queue = new PriorityQueue<>();
      	queue.add(new Path(s, 0));
      	while (queue.size() > 0) {
      	    Path p = queue.poll();
      	    if (costsS[p.value % 2][p.idx] <= p.value) {
      	        continue;
      	    }
      	    costsS[p.value % 2][p.idx] = p.value;
      	    for (int x : graph.get(p.idx)) {
      	        queue.add(new Path(x, p.value + 1));
      	    }
      	}
      	queue.add(new Path(g, 0));
      	while (queue.size() > 0) {
      	    Path p = queue.poll();
      	    if (costsG[p.value % 2][p.idx] <= p.value) {
      	        continue;
      	    }
      	    costsG[p.value % 2][p.idx] = p.value;
      	    for (int x : graph.get(p.idx)) {
      	        queue.add(new Path(x, p.value + 1));
      	    }
      	}
      	ArrayList<Integer> ans = new ArrayList<>();
      	for (int i = 0; i < n; i++) {
      	    if (costsS[0][i] + costsG[pp % 2][i] <= pp || costsS[1][i] + costsG[(pp + 1) % 2][i] <= pp) {
      	        ans.add(i + 1);
      	    }
      	}
      	if (ans.size() == 0) {
      	    System.out.println(-1);
      	} else {
      	    StringBuilder sb = new StringBuilder();
      	    sb.append(ans.size()).append("\n");
      	    for (int x : ans) {
      	        sb.append(x).append("\n");
      	    }
      	    System.out.print(sb);
      	}
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}

0