結果

問題 No.1812 Uribo Road
ユーザー 👑 emthrmemthrm
提出日時 2022-01-14 22:55:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 5,000 ms
コード長 3,989 bytes
コンパイル時間 2,817 ms
コンパイル使用メモリ 226,004 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-30 16:41:39
合計ジャッジ時間 3,887 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 37 ms
6,940 KB
testcase_13 AC 13 ms
6,940 KB
testcase_14 AC 12 ms
6,940 KB
testcase_15 AC 26 ms
6,940 KB
testcase_16 AC 25 ms
6,940 KB
testcase_17 AC 73 ms
6,940 KB
testcase_18 AC 13 ms
6,940 KB
testcase_19 AC 81 ms
6,944 KB
testcase_20 AC 80 ms
6,940 KB
testcase_21 AC 63 ms
6,944 KB
testcase_22 AC 48 ms
6,944 KB
testcase_23 AC 4 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 13 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 11 ms
6,940 KB
testcase_28 AC 22 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 3 ms
6,944 KB
testcase_31 AC 40 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 17 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename CostType>
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};

template <typename CostType>
struct Dijkstra {
  const CostType inf;

  Dijkstra(const std::vector<std::vector<Edge<CostType>>> &graph,
           const CostType inf = std::numeric_limits<CostType>::max())
  : graph(graph), inf(inf) {}

  std::vector<CostType> build(int s) {
    is_built = true;
    int n = graph.size();
    std::vector<CostType> dist(n, inf);
    dist[s] = 0;
    prev.assign(n, -1);
    using Pci = std::pair<CostType, int>;
    std::priority_queue<Pci, std::vector<Pci>, std::greater<Pci>> que;
    que.emplace(0, s);
    while (!que.empty()) {
      CostType cost; int ver; std::tie(cost, ver) = que.top(); que.pop();
      if (dist[ver] < cost) continue;
      for (const Edge<CostType> &e : graph[ver]) {
        if (dist[e.dst] > dist[ver] + e.cost) {
          dist[e.dst] = dist[ver] + e.cost;
          prev[e.dst] = ver;
          que.emplace(dist[e.dst], e.dst);
        }
      }
    }
    return dist;
  }

  std::vector<int> build_path(int t) const {
    assert(is_built);
    std::vector<int> res;
    for (; t != -1; t = prev[t]) res.emplace_back(t);
    std::reverse(res.begin(), res.end());
    return res;
  }

private:
  bool is_built = false;
  std::vector<std::vector<Edge<CostType>>> graph;
  std::vector<int> prev;
};

int main() {
  int n, m, k; cin >> n >> m >> k;
  vector<int> r(k); REP(i, k) cin >> r[i], --r[i];
  vector<vector<Edge<int>>> graph(n);
  vector<int> a(k), b(k), c(k), nodes{0, n - 1};
  REP(i, m) {
    int ai, bi, ci; cin >> ai >> bi >> ci; --ai; --bi;
    graph[ai].emplace_back(ai, bi, ci);
    graph[bi].emplace_back(bi, ai, ci);
    REP(j, k) {
      if (r[j] == i) {
        a[j] = ai;
        b[j] = bi;
        c[j] = ci;
        nodes.emplace_back(ai);
        nodes.emplace_back(bi);
      }
    }
  }
  sort(ALL(nodes));
  nodes.erase(unique(ALL(nodes)), nodes.end());
  const int x = nodes.size();
  vector dist(x, vector(x, 0));
  Dijkstra dijkstra(graph);
  REP(i, x) {
    const vector<int> d = dijkstra.build(nodes[i]);
    REP(j, x) dist[i][j] = d[nodes[j]];
  }
  vector dp(1 << k, vector(x, INF));
  REP(i, x) dp[0][i] = dist[0][i];
  REP(i, 1 << k) {
    REP(l, k) {
      if (i >> l & 1) continue;
      const int p = lower_bound(ALL(nodes), a[l]) - nodes.begin();
      const int q = lower_bound(ALL(nodes), b[l]) - nodes.begin();
      REP(j, x) {
        chmin(dp[i | (1 << l)][q], dp[i][j] + dist[j][p] + c[l]);
        chmin(dp[i | (1 << l)][p], dp[i][j] + dist[j][q] + c[l]);
      }
    }
  }
  int ans = INF;
  REP(i, x) chmin(ans, dp[(1 << k) - 1][i] + dist[i][x - 1]);
  cout << ans << '\n';
  return 0;
}
0