結果

問題 No.788 トラックの移動
コンテスト
ユーザー tnakao0123
提出日時 2019-02-10 03:44:16
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 2,062 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 927 ms
コンパイル使用メモリ 111,368 KB
実行使用メモリ 19,712 KB
最終ジャッジ日時 2026-03-25 22:17:40
合計ジャッジ時間 2,960 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 788.cc:  No.788 トラックの移動 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 2000;
const int INF = ~(1 << 31);
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;
typedef queue<int> qi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

int ts[MAX_N], ds[MAX_N][MAX_N];
vpii nbrs[MAX_N];

/* subroutines */

inline void setmin(ll &a, ll b) { if (a > b) a = b; }

/* main */

int main() {
  int n, m, l;
  scanf("%d%d%d", &n, &m, &l);
  l--;

  for (int i = 0; i < n; i++) scanf("%d", ts + i);

  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    nbrs[a].push_back(pii(b, c));
    nbrs[b].push_back(pii(a, c));
  }

  for (int st = 0; st < n; st++) {
    int *dists = ds[st];
    fill(dists, dists + n, INF);
    dists[st] = 0;

    priority_queue<pii> q;
    q.push(pii(0, st));

    while (! q.empty()) {
      pii u = q.top(); q.pop();
      int ud = -u.first, &ui = u.second;
      if (ud != dists[ui]) continue;

      vpii &nbru = nbrs[ui];
      for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
	int &vi = vit->first, vd = ud + vit->second;
	if (dists[vi] > vd) {
	  dists[vi] = vd;
	  q.push(pii(-vd, vi));
	}
      }
    }
  }

  ll minsum = LINF;
  for (int gl = 0; gl < n; gl++) {
    ll sum = 0;
    for (int i = 0; i < n; i++)
      sum += (ll)ds[gl][i] * ts[i] * 2;

    if (l == gl)
      setmin(minsum, sum);
    else if (ts[l] > 0)
      setmin(minsum, sum - ds[gl][l]);
    else // l != gl && ts[l] == 0
      for (int i = 0; i < n; i++)
	if (i != l && i != gl)
	  setmin(minsum, sum - ds[gl][i] + ds[l][i]);
  }

  printf("%lld\n", minsum);
  return 0;
}
0