結果

問題 No.17 2つの地点に泊まりたい
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-07-19 21:26:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,272 bytes
コンパイル時間 638 ms
コンパイル使用メモリ 60,440 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 02:14:52
合計ジャッジ時間 1,491 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

const int MAX_N = 51, INF = 1e8;
int s[MAX_N];
int dist[MAX_N][MAX_N];
//ワーシャルフロイド法 全点対間最短経路をもとめるとき) (0オリジン)
//dist[i][i] = 0 dist[i][j](経路がないもの)= dist[j][i] = INF(1e9)で初期化しておくこと
//dist[i][j] = dist[j][i] = (距離)を代入しておくこと
//この関数を利用することで、dist[i][j]の値(i,j間の距離)の最小値に更新されていく
void floyd (int n){//nは頂点の数
	rep(k, n) rep(i, n) rep(j, n)
		dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}

int main(void){
	rep(i, MAX_N)rep(j, MAX_N) dist[i][j] = dist[j][i] = INF;
	rep(i, MAX_N) dist[i][i] = 0;

	int n; cin >> n;
	rep(i, n) cin >> s[i];
	int m; cin >> m;
	rep(i, m){
		int a, b, c; cin >> a >> b >> c;
		dist[a][b] = dist[b][a] = c;
	}

	floyd(n);
	// rep(i, n)rep(j, n) printf("dist[%d][%d] = %d\n", i, j, dist[i][j]);

	int ans = INF, tmp;
	for (int i = 1; i < n - 1; ++i){
		for (int j = 1; j < n - 1; ++j){
			if(i != j){
				tmp = dist[0][i] + dist[i][j] + dist[j][n - 1] + s[i] + s[j];
				ans = min(ans, tmp);
			}
		}
	}
	cout << ans << endl;
	return 0;
}
0