結果

問題 No.2712 Play more!
ユーザー tenten
提出日時 2024-04-01 18:24:18
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,650 bytes
コンパイル時間 2,539 ms
コンパイル使用メモリ 80,400 KB
実行使用メモリ 54,864 KB
最終ジャッジ日時 2024-09-30 21:56:47
合計ジャッジ時間 6,396 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] values = new int[n];
        List<List<Path>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            graph.get(sc.nextInt() - 1).add(new Path(sc.nextInt() - 1, sc.nextInt()));
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        long[] points = new long[n];
        Arrays.fill(points, Long.MIN_VALUE);
        queue.add(new Path(0, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (p.value < Long.MAX_VALUE) {
                p.value += values[p.idx];
            }
            if (points[p.idx] >= p.value) {
                continue;
            }
            if (points[p.idx] > Long.MIN_VALUE) {
                p.value = Long.MAX_VALUE;
            }
            points[p.idx] = p.value;
            for (Path x : graph.get(p.idx)) {
                queue.add(new Path(x.idx, p.value - (p.value == Long.MAX_VALUE ? 0 : x.value)));
            }
        }
        System.out.println(points[n - 1] == Long.MAX_VALUE ? "inf" : points[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) {
            return Long.compare(another.value, value);
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0