結果

問題 No.788 トラックの移動
ユーザー tentententen
提出日時 2020-09-03 22:58:42
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,054 bytes
コンパイル時間 2,529 ms
コンパイル使用メモリ 83,552 KB
実行使用メモリ 73,680 KB
最終ジャッジ日時 2024-05-03 07:18:40
合計ジャッジ時間 15,437 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,681 ms
73,032 KB
testcase_01 AC 134 ms
41,308 KB
testcase_02 AC 135 ms
41,228 KB
testcase_03 AC 137 ms
41,412 KB
testcase_04 AC 765 ms
56,356 KB
testcase_05 AC 1,616 ms
73,680 KB
testcase_06 AC 1,731 ms
73,548 KB
testcase_07 AC 138 ms
41,312 KB
testcase_08 WA -
testcase_09 AC 144 ms
41,440 KB
testcase_10 AC 141 ms
41,204 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 844 ms
71,164 KB
testcase_16 AC 1,567 ms
73,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int m = sc.nextInt();
    	int start = sc.nextInt() - 1;
    	int[] trucks = new int[n];
    	ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    	for (int i = 0; i < n; i++) {
    	    trucks[i] = sc.nextInt();
    	    graph.add(new HashMap<>());
    	}
    	for (int i = 0; i < m; i++) {
    	    int a = sc.nextInt() - 1;
    	    int b = sc.nextInt() - 1;
    	    int c = sc.nextInt();
    	    graph.get(a).put(b, c);
    	    graph.get(b).put(a, c);
    	}
    	int[][] costs = new int[n][n];
    	PriorityQueue<Path> queue = new PriorityQueue<>();
    	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 (int x : graph.get(p.idx).keySet()) {
    	            if (costs[i][x] != Integer.MAX_VALUE) {
    	                continue;
    	            }
    	            queue.add(new Path(x, p.value + graph.get(p.idx).get(x)));
    	        }
    	    }
    	}
    	long min = Long.MAX_VALUE;
    	for (int i = 0; i < n; i++) {
    	    int other = Integer.MAX_VALUE;
    	    long base = 0;
    	    for (int j = 0; j < n; j++) {
    	        base += costs[i][j] * (long)trucks[j] * 2;
    	        if (trucks[j] != 0) {
    	            other = Math.min(other, costs[start][j] - costs[i][j]);
    	        }
    	    }
    	    min = Math.min(min, base + other);
    	}
    	System.out.println(min);
	}
	
	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;
	    }
	}
}
0