結果

問題 No.1449 新プロランド
ユーザー neko_the_shadowneko_the_shadow
提出日時 2021-04-08 11:36:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 7,403 bytes
コンパイル時間 2,851 ms
コンパイル使用メモリ 100,792 KB
実行使用メモリ 57,368 KB
最終ジャッジ日時 2023-08-25 16:52:46
合計ジャッジ時間 7,724 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
51,780 KB
testcase_01 AC 69 ms
51,908 KB
testcase_02 AC 105 ms
53,700 KB
testcase_03 AC 75 ms
51,612 KB
testcase_04 AC 79 ms
51,640 KB
testcase_05 AC 198 ms
56,964 KB
testcase_06 AC 170 ms
56,364 KB
testcase_07 AC 137 ms
55,752 KB
testcase_08 AC 173 ms
56,808 KB
testcase_09 AC 154 ms
55,900 KB
testcase_10 AC 145 ms
55,908 KB
testcase_11 AC 115 ms
53,852 KB
testcase_12 AC 191 ms
56,572 KB
testcase_13 AC 157 ms
56,028 KB
testcase_14 AC 99 ms
53,284 KB
testcase_15 AC 173 ms
56,708 KB
testcase_16 AC 154 ms
55,988 KB
testcase_17 AC 161 ms
56,344 KB
testcase_18 AC 70 ms
51,776 KB
testcase_19 AC 73 ms
51,844 KB
testcase_20 AC 69 ms
49,692 KB
testcase_21 AC 127 ms
53,820 KB
testcase_22 AC 89 ms
53,136 KB
testcase_23 AC 140 ms
55,760 KB
testcase_24 AC 146 ms
53,896 KB
testcase_25 AC 72 ms
51,596 KB
testcase_26 AC 70 ms
51,912 KB
testcase_27 AC 202 ms
57,368 KB
testcase_28 AC 179 ms
56,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package _1499;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.function.Supplier;
import java.util.regex.Pattern;

public class Main {
    public void exec() {
        int n = stdin.nextInt();
        int m = stdin.nextInt();
        int[] a = new int[m];
        int[] b = new int[m];
        long[] c = new long[m];
        for (int i = 0; i < m; i++) {
            a[i] = stdin.nextInt()-1;
            b[i] = stdin.nextInt()-1;
            c[i] = stdin.nextLong();
        }
        long[] t = stdin.nextLongArray(n);
        
        Dijkstra dijk = new Dijkstra(n, t);
        for (int i = 0; i < m; i++) {
            dijk.add(a[i], b[i], c[i]);
            dijk.add(b[i], a[i], c[i]);
        }
        dijk.run(0);
    }
    
    public class Dijkstra {
        private int n;
        private long m;
        private Map<Integer, List<Edge>> edges;
        private long[] T;

        public Dijkstra(int n, long[] T) {
            this.n = n;
            this.edges = new HashMap<>();
            this.T = T;
            this.m = 0;
        }

        public void add(int from, int to, long cost) {
            this.edges.computeIfAbsent(from, unused -> new ArrayList<>()).add(new Edge(to, cost));
            this.m = Math.max(m, cost);
        }

        public void run(int start) {
            long[][] score = new long[n][(int)m+2];
            for (long[] row : score) Arrays.fill(row, Long.MAX_VALUE);
            score[start][0] = 0;

            PriorityQueue<Tuple> q = new PriorityQueue<>();
            q.add(new Tuple(start, 0, 0));
            while (!q.isEmpty()) {
                Tuple t = q.remove();
                if (!edges.containsKey(t.current)) continue; // 次がない場合
                if (score[t.current][(int)t.sum] < t.cost) continue; // cost情報が古い場合
                
                long nxt = Math.min(T[t.current] + t.sum, m+1);
                for (Edge e : edges.get(t.current)) {
                    if (t.cost + T[t.current] + e.cost/(T[t.current]+t.sum) < score[e.next][(int)nxt]) {
                        score[e.next][(int)nxt] = t.cost + T[t.current] + e.cost/(T[t.current]+t.sum);
                        q.add(new Tuple(e.next, score[e.next][(int)nxt], nxt));
                    }
                }
            }
           
            
            long ans = Arrays.stream(score[n-1]).min().getAsLong();
            stdout.println(ans);
        }

        private class Edge {
            private int next;
            private long cost;

            public Edge(int next, long cost) {
                this.next = next;
                this.cost = cost;
            }
        }

        private class Tuple implements Comparable<Tuple>{
            private int current;
            private long cost;
            private long sum;

            public Tuple(int current, long cost, long sum) {
                this.current = current;
                this.cost = cost;
                this.sum = sum;
            }

            @Override
            public int compareTo(Tuple other) {
                return Long.compare(this.cost, other.cost);
            }
        }
    }


    private static final Stdin stdin = new Stdin(System.in);
    private static final Stdout stdout = new Stdout(System.out);

    public static void main(String[] args) {
        try {
            new Main().exec();
        } finally {
            stdout.flush();
        }
    }

    public static class Stdin {
        private Deque<String> queue;
        private BufferedReader in;
        private Pattern space;

        public Stdin(InputStream in) {
            this.queue = new ArrayDeque<>();
            this.in = new BufferedReader(new InputStreamReader(in));
            this.space = Pattern.compile(" ");
        }

        public String nextString() {
            if (queue.isEmpty()) {
                try {
                    String line = in.readLine();
                    if (line == null) {
                        throw new EOFException();
                    }
                    space.splitAsStream(line).forEach(this.queue::addLast);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return queue.removeFirst();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

        public long nextLong() {
            return Long.parseLong(nextString());
        }
        
        public BigInteger nextBigInteger() {
            return new BigInteger(nextString());
        }

        public String[] nextStringArray(int n) {
            String[] a = new String[n];
            for (int i = 0; i < n; i++) a[i] = nextString();
            return a;
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public double[] nextDoubleArray(int n) {
            double[] a = new double[n];
            for (int i = 0; i < n; i++) a[i] = nextDouble();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
        
        public BigInteger[] nexBigIntegerArray(int n) {
            BigInteger[] a = new BigInteger[n];
            for (int i = 0; i < n; i++) a[i] = nextBigInteger();
            return a;
        }
        
        public List<Integer> nextIntegerList(int n) {
            return nextList(n, this::nextInt);
        }
        
        public List<Long> nextLongList(int n) {
            return nextList(n, this::nextLong);
        }
        
        public List<Double> nextDoubleList(int n) {
            return nextList(n, this::nextDouble);
        }
        
        public List<String> nextStringList(int n) {
            return nextList(n, this::nextString);
        }
        
        public List<BigInteger> nextBigIntegerList(int n) {
            return nextList(n, this::nextBigInteger);
        }
        
        private <T> List<T> nextList(int n, Supplier<T> supplier) {
            List<T> a = new ArrayList<>();
            for (int i = 0; i < n; i++) a.add(supplier.get());
            return a;
        }
    }

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout(PrintStream stdout) {
            this.stdout =  new PrintWriter(stdout, false);
        }

        public void println(Object ... objs) {
            for (int i = 0, len = objs.length; i < len; i++) {
                stdout.print(objs[i]);
                if (i != len-1) stdout.print(' ');
            }
            stdout.println();
        }

        public void flush() {
            stdout.flush();
        }
    }
}
0