結果

問題 No.1676 Coin Trade (Single)
ユーザー tentententen
提出日時 2022-07-27 10:25:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 478 ms / 2,000 ms
コード長 2,678 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 81,552 KB
実行使用メモリ 65,580 KB
最終ジャッジ日時 2023-09-23 17:22:48
合計ジャッジ時間 12,280 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,448 KB
testcase_01 AC 41 ms
49,584 KB
testcase_02 AC 44 ms
49,784 KB
testcase_03 AC 429 ms
65,492 KB
testcase_04 AC 390 ms
63,020 KB
testcase_05 AC 393 ms
62,796 KB
testcase_06 AC 376 ms
62,956 KB
testcase_07 AC 357 ms
61,224 KB
testcase_08 AC 274 ms
60,688 KB
testcase_09 AC 353 ms
60,428 KB
testcase_10 AC 362 ms
62,512 KB
testcase_11 AC 441 ms
65,356 KB
testcase_12 AC 316 ms
61,096 KB
testcase_13 AC 45 ms
49,328 KB
testcase_14 AC 44 ms
49,508 KB
testcase_15 AC 44 ms
49,456 KB
testcase_16 AC 44 ms
49,384 KB
testcase_17 AC 43 ms
49,472 KB
testcase_18 AC 42 ms
49,352 KB
testcase_19 AC 46 ms
49,356 KB
testcase_20 AC 44 ms
49,936 KB
testcase_21 AC 43 ms
49,420 KB
testcase_22 AC 43 ms
49,580 KB
testcase_23 AC 44 ms
49,504 KB
testcase_24 AC 42 ms
49,408 KB
testcase_25 AC 43 ms
49,952 KB
testcase_26 AC 43 ms
49,368 KB
testcase_27 AC 45 ms
49,896 KB
testcase_28 AC 45 ms
49,556 KB
testcase_29 AC 43 ms
49,764 KB
testcase_30 AC 44 ms
49,552 KB
testcase_31 AC 44 ms
49,728 KB
testcase_32 AC 45 ms
49,360 KB
testcase_33 AC 472 ms
65,576 KB
testcase_34 AC 478 ms
65,100 KB
testcase_35 AC 478 ms
65,580 KB
testcase_36 AC 469 ms
65,120 KB
testcase_37 AC 478 ms
65,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        ArrayList<ArrayList<Path>> graph = new ArrayList<>();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
            values[i] = sc.nextInt();
            int m = sc.nextInt();
            for (int j = 0; j < m; j++) {
                int idx = sc.nextInt() - 1;
                if (values[idx] < values[i]) {
                    graph.get(idx).add(new Path(i, values[i] - values[idx]));
                }
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        long[] gains = new long[n];
        Arrays.fill(gains, -1);
        queue.add(new Path(0, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (gains[p.idx] >= p.value) {
                continue;
            }
            gains[p.idx] = p.value;
            for (Path x : graph.get(p.idx)) {
                queue.add(new Path(x.idx, x.value + p.value));
            }
            if (p.idx < n - 1) {
                queue.add(new Path(p.idx + 1, p.value));
            }
        }
        System.out.println(gains[n - 1]);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        long value;
        
        public Path(int idx, long value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (idx == another.idx) {
                if (value == another.value) {
                    return 0;
                } else if (value < another.value) {
                    return 1;
                } else {
                    return -1;
                }
            } else {
                return idx - another.idx;
            }
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0