結果

問題 No.788 トラックの移動
ユーザー simansiman
提出日時 2023-07-06 23:17:16
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 882 ms / 2,000 ms
コード長 2,021 bytes
コンパイル時間 1,465 ms
コンパイル使用メモリ 106,600 KB
実行使用メモリ 35,284 KB
最終ジャッジ日時 2023-09-28 00:20:08
合計ジャッジ時間 6,846 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 882 ms
35,284 KB
testcase_01 AC 10 ms
35,104 KB
testcase_02 AC 8 ms
34,968 KB
testcase_03 AC 9 ms
35,068 KB
testcase_04 AC 191 ms
35,156 KB
testcase_05 AC 800 ms
35,140 KB
testcase_06 AC 820 ms
35,184 KB
testcase_07 AC 10 ms
35,052 KB
testcase_08 AC 9 ms
35,020 KB
testcase_09 AC 8 ms
35,096 KB
testcase_10 AC 8 ms
35,092 KB
testcase_11 AC 10 ms
35,008 KB
testcase_12 AC 10 ms
35,024 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 165 ms
35,252 KB
testcase_16 AC 684 ms
35,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

struct Node {
  int v;
  ll cost;

  Node(int v = -1, ll cost = -1) {
    this->v = v;
    this->cost = cost;
  }

  bool operator>(const Node &n) const {
    return cost > n.cost;
  }
};

struct Edge {
  int to;
  ll cost;

  Edge(int to, int cost) {
    this->to = to;
    this->cost = cost;
  }
};

ll g_cost[2010][2010];

int main() {
  int N, M, L;
  cin >> N >> M >> L;

  int track_cnt = 0;
  int T[N];
  for (int i = 0; i < N; ++i) {
    cin >> T[i];
    track_cnt += T[i];
  }

  if (track_cnt == 1) {
    cout << 0 << endl;
    return 0;
  }

  memset(g_cost, 0, sizeof(g_cost));

  vector<Edge> G[N + 1];
  for (int i = 0; i < M; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;

    G[a].push_back(Edge(b, c));
    G[b].push_back(Edge(a, c));
  }

  for (int v = 1; v <= N; ++v) {
    priority_queue <Node, vector<Node>, greater<Node>> pque;
    bool visited[N + 1];
    memset(visited, false, sizeof(visited));

    pque.push(Node(v, 0));
    while (not pque.empty()) {
      Node node = pque.top();
      pque.pop();

      if (visited[node.v]) continue;
      visited[node.v] = true;

      g_cost[v][node.v] = node.cost;

      for (Edge &e : G[node.v]) {
        ll n_cost = node.cost + e.cost;
        pque.push(Node(e.to, n_cost));
      }
    }
  }

  ll ans = LLONG_MAX;

  for (int v = 1; v <= N; ++v) {
    ll base_cost = 0;

    for (int u = 1; u <= N; ++u) {
      base_cost += T[u - 1] * (2 * g_cost[u][v]);
    }

    ll min_cost = LLONG_MAX;

    for (int u = 1; u <= N; ++u) {
      if (T[u - 1] == 0) continue;

      ll new_cost = base_cost;
      new_cost -= 2 * g_cost[u][v];
      new_cost += g_cost[L][u] + g_cost[u][v];

      min_cost = min(min_cost, new_cost);
    }

    ans = min(ans, min_cost);
  }

  cout << ans << endl;

  return 0;
}
0