結果

問題 No.17 2つの地点に泊まりたい
ユーザー GrenacheGrenache
提出日時 2015-11-03 15:30:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 111 ms / 5,000 ms
コード長 3,155 bytes
コンパイル時間 4,026 ms
コンパイル使用メモリ 79,396 KB
実行使用メモリ 39,284 KB
最終ジャッジ日時 2024-04-23 02:09:05
合計ジャッジ時間 6,863 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
37,152 KB
testcase_01 AC 55 ms
37,120 KB
testcase_02 AC 56 ms
37,352 KB
testcase_03 AC 69 ms
37,680 KB
testcase_04 AC 64 ms
37,500 KB
testcase_05 AC 91 ms
38,304 KB
testcase_06 AC 86 ms
38,268 KB
testcase_07 AC 86 ms
38,216 KB
testcase_08 AC 101 ms
38,736 KB
testcase_09 AC 111 ms
39,284 KB
testcase_10 AC 76 ms
37,800 KB
testcase_11 AC 100 ms
38,492 KB
testcase_12 AC 59 ms
37,300 KB
testcase_13 AC 55 ms
37,328 KB
testcase_14 AC 57 ms
37,404 KB
testcase_15 AC 55 ms
37,328 KB
testcase_16 AC 57 ms
36,976 KB
testcase_17 AC 53 ms
36,956 KB
testcase_18 AC 59 ms
37,384 KB
testcase_19 AC 58 ms
37,184 KB
testcase_20 AC 54 ms
37,076 KB
testcase_21 AC 53 ms
36,852 KB
testcase_22 AC 63 ms
37,376 KB
testcase_23 AC 87 ms
38,704 KB
testcase_24 AC 86 ms
38,760 KB
testcase_25 AC 60 ms
37,384 KB
testcase_26 AC 108 ms
38,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;


public class Main_yukicoder17 {

    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Printer pr = new Printer(System.out);

		int n = sc.nextInt();

		int[] s = new int[n];
		for (int i = 0; i < n; i++) {
			s[i] = sc.nextInt();
		}

		FloydWarshall fw = new FloydWarshall(n);
		int m = sc.nextInt();
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			int c = sc.nextInt();
			fw.addEdge(a, b, c);
			fw.addEdge(b, a, c);
		}

		long min = Long.MAX_VALUE;
		for (int i = 1; i < n - 1; i++) {
			for (int j = 1; j < n - 1; j++) {
				if (i == j) {
					continue;
				}
				long tmp = fw.getShortestPath(0, i) + fw.getShortestPath(i, j) + fw.getShortestPath(j, n - 1);
				tmp += s[i] + s[j];
				min = Math.min(min, tmp);
			}
		}

		pr.println(min);

		pr.close();
		sc.close();
	}
	
	private static class FloydWarshall {
		long[][] d;
		int n;
		long[][] result;
		boolean nf; // NEGATIVE CYCLE flag

		final static long INF = Long.MAX_VALUE / 4;

		FloydWarshall(int n) {
			this.n = n;
			d = new long[n][n];
			for (int i = 0; i < n; i++) {
				Arrays.fill(d[i], INF);
				d[i][i] = 0;
			}
			nf = false;
		}

		// i, j:0-indexed
		public void addEdge(int i, int j, int c) {
			d[i][j] = c;
		}

		public long getShortestPath(int i, int j) {
			if (nf) {
				return -INF;
			}

			if (result == null) {
				for (int kk = 0; kk < n; kk++) {
					for (int ii = 0; ii < n; ii++) {
						for (int jj = 0; jj < n; jj++) {
//							d[ii][jj] = Math.min(d[ii][jj], d[ii][kk] + d[kk][jj]);
							if (d[ii][kk] != INF && d[kk][jj] != INF && d[ii][jj] > d[ii][kk] + d[kk][jj]) {
								d[ii][jj] = d[ii][kk] + d[kk][jj];
							}
						}
					}
				}
				
				for (int k = 0; k < n; k++) {
					if (d[k][k] < 0) {
						nf = true;
						return -INF;
					}
				}
				
				result = d;
			}
			
			return result[i][j];
		}
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;
		
		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}
		
		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}
		
		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0