結果

問題 No.788 トラックの移動
ユーザー htensaihtensai
提出日時 2020-05-14 19:54:37
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,061 bytes
コンパイル時間 3,710 ms
コンパイル使用メモリ 77,068 KB
実行使用メモリ 84,316 KB
最終ジャッジ日時 2023-10-13 23:16:30
合計ジャッジ時間 15,776 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,514 ms
84,092 KB
testcase_01 AC 119 ms
55,624 KB
testcase_02 AC 117 ms
55,988 KB
testcase_03 AC 118 ms
55,788 KB
testcase_04 AC 628 ms
66,656 KB
testcase_05 AC 1,467 ms
84,188 KB
testcase_06 AC 1,540 ms
84,316 KB
testcase_07 AC 123 ms
55,824 KB
testcase_08 WA -
testcase_09 AC 122 ms
55,964 KB
testcase_10 AC 125 ms
55,748 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 116 ms
55,376 KB
testcase_14 AC 114 ms
53,720 KB
testcase_15 AC 796 ms
82,964 KB
testcase_16 AC 1,367 ms
83,936 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 l = sc.nextInt() - 1;
		int[] trucks = new int[n];
		ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
		int count = 0;
		int[][] costs = new int[n][n];
		for (int i = 0; i < n; i++) {
		    trucks[i] = sc.nextInt();
		    if (trucks[i] > 0) {
		        count++;
		    }
		    graph.add(new HashMap<>());
		    Arrays.fill(costs[i], Integer.MAX_VALUE);
		}
		if (count <= 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).put(b, c);
		    graph.get(b).put(a, c);
		}
		PriorityQueue<Path> queue = new PriorityQueue<>();
		for (int i = 0; i < n; i++) {
		    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 (Map.Entry<Integer, Integer> entry : graph.get(p.idx).entrySet()) {
		            if (costs[i][entry.getKey()] == Integer.MAX_VALUE) {
		                queue.add(new Path(entry.getKey(), entry.getValue() + p.value));
		            }
		        }
		    }
		}
		long min = Long.MAX_VALUE;
		for (int i = 0; i < n; i++) {
		    long max = Long.MIN_VALUE;
		    long total = 0;
		    for (int j = 0; j < n; j++) {
		        if (trucks[j] > 0) {
		            total += costs[i][j] * (long)trucks[j] * 2;
		            max = Math.max(max, costs[i][j] - costs[j][l]);
		        }
		    }
		    min = Math.min(min, total - max);
		}
		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