結果

問題 No.17 2つの地点に泊まりたい
ユーザー siman
提出日時 2016-03-22 02:25:22
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,336 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 73,892 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-15 01:32:37
合計ジャッジ時間 1,330 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <limits.h>
#include <time.h>
#include <string>
#include <string.h>
#include <sstream>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;

typedef long long ll;

const int MAX_N = 50;
const int INF = 999999;

int dist[MAX_N][MAX_N];
int stayCost[MAX_N];

void init(){
  for(int i = 0; i < MAX_N; i++){
    for(int j = 0; j < MAX_N; j++){
      dist[i][j] = INF;
    }
  }
}

int n;

void warshall_floyd() {
  for(int k = 0; k < n; k++){
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
}

int main(){
  init();

  cin >> n;

  for(int i = 0; i < n; i++){
    cin >> stayCost[i];
    dist[i][i] = 0;
  }

  int m;
  cin >> m;

  int from, to, cost;
  for(int i = 0; i < m; i++){
    cin >> from >> to >> cost;
    dist[from][to] = cost;
    dist[to][from] = cost;
  }

  warshall_floyd();

  int minCost = INT_MAX;

  for(int i = 1; i < n-1; i++){
    for(int j = 1; j < n-1; j++){
      if(i == j) continue;
      int cost = dist[0][i] + stayCost[i] + dist[i][j] + stayCost[j] + dist[j][n-1];

      minCost = min(minCost, cost);
    }
  }

  cout << minCost << endl;

  return 0;
}
0