結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
jp_ste
|
| 提出日時 | 2016-02-29 15:12:56 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,172 bytes |
| コンパイル時間 | 2,541 ms |
| コンパイル使用メモリ | 79,720 KB |
| 実行使用メモリ | 59,728 KB |
| 最終ジャッジ日時 | 2024-09-24 12:48:35 |
| 合計ジャッジ時間 | 21,983 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 WA * 12 |
ソースコード
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int N, M;
static int stayCost[];
static int moveCost[][];
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
N = scan.nextInt();
stayCost = new int[N];
for(int i=0; i<N; i++) {
stayCost[i] = scan.nextInt();
}
M = scan.nextInt();
moveCost = new int[N][N];
for(int i=0; i<M; i++) {
int from = scan.nextInt();
int to = scan.nextInt();
int cost = scan.nextInt();
moveCost[from][to] = cost;
moveCost[to][from] = cost;
}
int ans = Integer.MAX_VALUE;
for(int i=1; i<N-1; i++) {
for(int j=1; j<N-1; j++) {
if(i==j) continue;
int v1 = calcMinCost(i, 0);
if(v1 == Integer.MAX_VALUE) continue;
int v2 = calcMinCost(j, i);
if(v2 == Integer.MAX_VALUE) continue;
int v3 = calcMinCost(N-1, j);
if(v3 == Integer.MAX_VALUE) continue;
int tmp = v1+v2+v3;
ans = Math.min(ans, tmp);
}
}
System.out.println(ans);
}
}
static int calcMinCost(int start, int goal) {
int minCost[] = new int[N];
for(int i=0; i<N; i++) {
minCost[i] = Integer.MAX_VALUE;
}
minCost[start] = stayCost[start];
boolean flag[] = new boolean[N];
Queue<Integer> q = new LinkedList<>();
q.add(start);
while(true) {
Integer now = q.poll();
if(now == null) break;
flag[now] = true;
for(int i=0; i<N; i++) {
if(i==now) continue;
if(moveCost[now][i] > 0 && flag[i] == false) {
int tmp = minCost[now] + moveCost[now][i];
minCost[i] = Math.min(minCost[i], tmp);
q.add(i);
}
}
}
return minCost[goal];
}
}
jp_ste