結果

問題 No.788 トラックの移動
ユーザー tentententen
提出日時 2022-08-24 10:43:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,763 ms / 2,000 ms
コード長 3,802 bytes
コンパイル時間 4,094 ms
コンパイル使用メモリ 74,632 KB
実行使用メモリ 80,312 KB
最終ジャッジ日時 2023-08-21 17:42:20
合計ジャッジ時間 14,633 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,682 ms
80,092 KB
testcase_01 AC 45 ms
49,464 KB
testcase_02 AC 46 ms
49,644 KB
testcase_03 AC 46 ms
49,600 KB
testcase_04 AC 535 ms
64,584 KB
testcase_05 AC 1,580 ms
80,092 KB
testcase_06 AC 1,763 ms
80,312 KB
testcase_07 AC 47 ms
49,444 KB
testcase_08 AC 46 ms
49,436 KB
testcase_09 AC 47 ms
49,456 KB
testcase_10 AC 47 ms
49,560 KB
testcase_11 AC 49 ms
49,664 KB
testcase_12 AC 49 ms
50,148 KB
testcase_13 AC 44 ms
49,200 KB
testcase_14 AC 44 ms
49,392 KB
testcase_15 AC 596 ms
79,756 KB
testcase_16 AC 1,354 ms
80,004 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 m = sc.nextInt();
        int k = sc.nextInt() - 1;
        long[] tracks = new long[n];
        int zeros = 0;
        ArrayList<ArrayList<Path>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            tracks[i] = sc.nextInt();
            graph.add(new ArrayList<>());
            if (tracks[i] == 0) {
                zeros++;
            }
        }
        if (zeros >= n - 1) {
            System.out.println(0);
            return;
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            graph.get(a).add(new Path(b, c));
            graph.get(b).add(new Path(a, c));
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        int[][] costs = new int[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(costs[i], Integer.MAX_VALUE);
            queue.add(new Path(i, 0));
            while (queue.size() > 0) {
                Path p = queue.poll();
                if (costs[i][p.idx] <= p.value) {
                    continue;
                }
                costs[i][p.idx] = p.value;
                for (Path x : graph.get(p.idx)) {
                    queue.add(new Path(x.idx, x.value + p.value));
                }
            }
        }
        long ans = Long.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            long sum = 0;
            long start = Long.MAX_VALUE;
            for (int j = 0; j < n; j++) {
                sum += costs[i][j] * tracks[j] * 2;
                if (tracks[j] > 0) {
                    start = Math.min(start, costs[j][k] - costs[i][j]);
                }
            }
            ans = Math.min(ans, start + sum);
        }
        System.out.println(ans);
    }
    
    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;
        }
    }
}
class BinaryIndexedTree {
    int size;
    int[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new int[size];
    }
    
    public void add(int idx, int value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public int getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    public int getSum(int x) {
        int mask = 1;
        int ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
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