結果

問題 No.1190 Points
ユーザー tentententen
提出日時 2020-08-24 22:39:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,941 ms / 2,000 ms
コード長 2,552 bytes
コンパイル時間 2,689 ms
コンパイル使用メモリ 80,484 KB
実行使用メモリ 82,048 KB
最終ジャッジ日時 2024-04-24 03:58:27
合計ジャッジ時間 38,987 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
41,332 KB
testcase_01 AC 135 ms
41,312 KB
testcase_02 AC 137 ms
41,148 KB
testcase_03 AC 1,427 ms
70,564 KB
testcase_04 AC 1,297 ms
68,212 KB
testcase_05 AC 1,191 ms
66,252 KB
testcase_06 AC 1,573 ms
73,708 KB
testcase_07 AC 1,617 ms
75,032 KB
testcase_08 AC 1,448 ms
75,180 KB
testcase_09 AC 1,510 ms
74,380 KB
testcase_10 AC 1,417 ms
75,576 KB
testcase_11 AC 1,380 ms
72,972 KB
testcase_12 AC 1,440 ms
75,080 KB
testcase_13 AC 1,491 ms
71,640 KB
testcase_14 AC 579 ms
52,668 KB
testcase_15 AC 1,625 ms
76,740 KB
testcase_16 AC 489 ms
49,952 KB
testcase_17 AC 1,580 ms
74,348 KB
testcase_18 AC 1,463 ms
75,124 KB
testcase_19 AC 447 ms
51,408 KB
testcase_20 AC 1,498 ms
74,688 KB
testcase_21 AC 753 ms
54,692 KB
testcase_22 AC 711 ms
56,432 KB
testcase_23 AC 1,747 ms
82,048 KB
testcase_24 AC 1,941 ms
79,656 KB
testcase_25 AC 1,606 ms
79,136 KB
testcase_26 AC 1,037 ms
70,316 KB
testcase_27 AC 1,742 ms
77,248 KB
権限があれば一括ダウンロードができます

ソースコード

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