提出 #69142074


ソースコード 拡げる

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Main {

    record ParetoEntry(long fuel, long weight) {
    }

    record State(long fuel, long weight, int vertex) {
    }

    public static void main(String[] args) throws Exception {
        BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String[] s = bu.readLine().split(" ");
        int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]);
        int[] w = new int[n];
        s = bu.readLine().split(" ");
        for (int i = 0; i < n; i++) w[i] = Integer.parseInt(s[i]);
        ArrayList<Integer>[] adj = new ArrayList[n];
        for (int i = 0; i < n; i++) adj[i] = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            s = bu.readLine().split(" ");
            int u = Integer.parseInt(s[0]) - 1, v = Integer.parseInt(s[1]) - 1;
            adj[u].add(v);
            adj[v].add(u);
        }
        ArrayList<ParetoEntry>[] minCosts = new ArrayList[n];
        for (int i = 0; i < n; i++) minCosts[i] = new ArrayList<>();
        PriorityQueue<State> pq = new PriorityQueue<>(Comparator.comparingLong(State::fuel));
        long initialW = w[0];
        minCosts[0].add(new ParetoEntry(0, initialW));
        pq.add(new State(0, initialW, 0));
        while (!pq.isEmpty()) {
            State cur = pq.poll();
            long curFuel = cur.fuel(), curWeight = cur.weight();
            int u = cur.vertex();
            for (int v : adj[u]) {
                long newFuel = curFuel + curWeight, newWeight = curWeight + w[v];
                if (!isDominated(minCosts[v], newFuel, newWeight)) {
                    addAndPrune(minCosts[v], newFuel, newWeight);
                    pq.add(new State(newFuel, newWeight, v));
                }
            }
        }
        for (int i = 0; i < n; i++) {
            long minFuel = Long.MAX_VALUE;
            for (ParetoEntry entry : minCosts[i]) minFuel = Math.min(minFuel, entry.fuel());
            sb.append(minFuel + "\n");
        }
        System.out.print(sb);
    }

    private static void addAndPrune(ArrayList<ParetoEntry> minCost, long newFuel, long newWeight) {
        minCost.removeIf(entry -> newFuel <= entry.fuel() && newWeight <= entry.weight());
        minCost.add(new ParetoEntry(newFuel, newWeight));
    }

    private static boolean isDominated(ArrayList<ParetoEntry> minCost, long curFuel, long curWeight) {
        for (ParetoEntry entry : minCost) if (entry.fuel() <= curFuel && entry.weight() <= curWeight) return true;
        return false;
    }
}

提出情報

提出日時
問題 F - Eat and Ride
ユーザ uttaran_das
言語 Java (OpenJDK 17)
得点 500
コード長 2782 Byte
結果 AC
実行時間 245 ms
メモリ 48492 KiB

コンパイルエラー

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 51
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 67 ms 37588 KiB
00_sample_01.txt AC 66 ms 37744 KiB
00_sample_02.txt AC 67 ms 37744 KiB
01_random_03.txt AC 124 ms 40936 KiB
01_random_04.txt AC 124 ms 40968 KiB
01_random_05.txt AC 128 ms 40756 KiB
01_random_06.txt AC 114 ms 40052 KiB
01_random_07.txt AC 112 ms 39340 KiB
01_random_08.txt AC 132 ms 41920 KiB
01_random_09.txt AC 124 ms 40440 KiB
01_random_10.txt AC 99 ms 39300 KiB
01_random_11.txt AC 158 ms 43216 KiB
01_random_12.txt AC 150 ms 42544 KiB
01_random_13.txt AC 146 ms 42744 KiB
01_random_14.txt AC 155 ms 43236 KiB
01_random_15.txt AC 146 ms 42784 KiB
01_random_16.txt AC 147 ms 42700 KiB
01_random_17.txt AC 153 ms 43208 KiB
01_random_18.txt AC 151 ms 42508 KiB
01_random_19.txt AC 151 ms 43036 KiB
01_random_20.txt AC 158 ms 43384 KiB
01_random_21.txt AC 147 ms 43328 KiB
01_random_22.txt AC 162 ms 43388 KiB
01_random_23.txt AC 138 ms 42996 KiB
01_random_24.txt AC 147 ms 43156 KiB
01_random_25.txt AC 141 ms 42932 KiB
01_random_26.txt AC 162 ms 43432 KiB
01_random_27.txt AC 157 ms 43544 KiB
01_random_28.txt AC 150 ms 42860 KiB
01_random_29.txt AC 147 ms 42532 KiB
01_random_30.txt AC 159 ms 43476 KiB
01_random_31.txt AC 120 ms 40512 KiB
01_random_32.txt AC 139 ms 41408 KiB
01_random_33.txt AC 128 ms 40588 KiB
01_random_34.txt AC 141 ms 41940 KiB
01_random_35.txt AC 132 ms 41648 KiB
01_random_36.txt AC 133 ms 41576 KiB
01_random_37.txt AC 146 ms 42812 KiB
01_random_38.txt AC 138 ms 41264 KiB
01_random_39.txt AC 125 ms 41288 KiB
01_random_40.txt AC 123 ms 40572 KiB
01_random_41.txt AC 227 ms 48492 KiB
01_random_42.txt AC 237 ms 48424 KiB
01_random_43.txt AC 235 ms 45924 KiB
01_random_44.txt AC 237 ms 48260 KiB
01_random_45.txt AC 245 ms 48384 KiB
01_random_46.txt AC 140 ms 41856 KiB
01_random_47.txt AC 133 ms 43200 KiB
01_random_48.txt AC 151 ms 42864 KiB
01_random_49.txt AC 161 ms 42888 KiB
01_random_50.txt AC 153 ms 43612 KiB