結果

問題 No.1812 Uribo Road
ユーザー miscalcmiscalc
提出日時 2022-01-14 23:51:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 5,000 ms
コード長 4,449 bytes
コンパイル時間 2,659 ms
コンパイル使用メモリ 227,964 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-11-20 15:57:14
合計ジャッジ時間 4,870 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,824 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 8 ms
6,816 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 3 ms
6,820 KB
testcase_09 AC 4 ms
6,820 KB
testcase_10 AC 3 ms
6,820 KB
testcase_11 AC 3 ms
6,816 KB
testcase_12 AC 58 ms
7,296 KB
testcase_13 AC 35 ms
7,040 KB
testcase_14 AC 33 ms
6,816 KB
testcase_15 AC 42 ms
6,816 KB
testcase_16 AC 35 ms
6,816 KB
testcase_17 AC 96 ms
7,040 KB
testcase_18 AC 31 ms
6,816 KB
testcase_19 AC 122 ms
7,936 KB
testcase_20 AC 127 ms
8,064 KB
testcase_21 AC 98 ms
8,064 KB
testcase_22 AC 81 ms
7,936 KB
testcase_23 AC 5 ms
6,820 KB
testcase_24 AC 2 ms
6,816 KB
testcase_25 AC 20 ms
6,816 KB
testcase_26 AC 3 ms
6,820 KB
testcase_27 AC 22 ms
6,820 KB
testcase_28 AC 34 ms
6,820 KB
testcase_29 AC 2 ms
6,816 KB
testcase_30 AC 5 ms
6,820 KB
testcase_31 AC 65 ms
6,820 KB
testcase_32 AC 3 ms
6,816 KB
testcase_33 AC 30 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/modint>
using namespace atcoder;
//using mint = modint998244353;
using mint = modint1000000007;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tlll = tuple<ll, ll, ll>;
constexpr ll INF = 1LL << 60;
template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
ll safemod(ll A, ll M) {return (A % M + M) % M;}
ll divfloor(ll A, ll B) {if (B < 0) {return divfloor(-A, -B);} return (A - safemod(A, B)) / B;}
ll divceil(ll A, ll B) {if (B < 0) {return divceil(-A, -B);} return divfloor(A + B - 1, B);}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)

template<class T> class graph
{
public:
  struct edge
  {
    ll to;
    T cost;
    edge(ll to, T cost) : to(to), cost(cost) {}
  };

  ll N;
  vector<vector<edge>> G;

  graph(ll n)
  {
    N = n;
    G.resize(N);
  }

  void connect(ll sv, ll gv, T c)
  {
    G.at(sv).push_back(edge(gv, c));
  }

  void connect2(ll v0, ll v1, T c)
  {
    connect(v0, v1, c), connect(v1, v0, c);
  }

  vector<T> _01bfs(ll sv)
  {
    vector<T> costs(N, INF);
    costs.at(sv) = 0;
    deque<tuple<T, ll, ll>> deq;
    for (auto e : G.at(sv))
    {
      if (e.cost == 0)
        deq.push_front(make_tuple(e.cost, sv, e.to));
      else
        deq.push_back(make_tuple(e.cost, sv, e.to));
    }

    while (!deq.empty())
    {
      auto [c, pv, v] = deq.front();
      deq.pop_front();

      if (chmin(costs.at(v), costs.at(pv) + c))
      {
        for (auto e : G.at(v))
        {
          if (e.cost == 0)
            deq.push_front(make_tuple(e.cost, v, e.to));
          else
            deq.push_back(make_tuple(e.cost, v, e.to));
        }
      }
    }
    return costs;
  }

  vector<T> dijkstra(ll sv)
  {
    vector<T> costs(N, INF);
    costs.at(sv) = 0;
    priority_queue<pair<T, ll>, vector<pair<T, ll>>, greater<pair<T, ll>>> pque;
    pque.emplace(make_pair(0, sv));

    while (!pque.empty())
    {
      auto [c, v] = pque.top();
      pque.pop();

      if (costs.at(v) < c)
        continue;
      
      for (auto e : G.at(v))
      {
        T nc = c + e.cost;
        if (costs.at(e.to) > nc)
        {
          costs.at(e.to) = nc;
          pque.emplace(nc, e.to);
        }
      }
    }
    return costs;
  }
};

int main()
{
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> R(K);
  for (ll i = 0; i < K; i++)
  {
    cin >> R.at(i);
    R.at(i)--;
  }

  graph<ll> gr(N);
  vector<ll> A(M), B(M), C(M);
  for (ll i = 0; i < M; i++)
  {
    ll a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    A.at(i) = a, B.at(i) = b, C.at(i) = c;
    gr.connect2(a, b, c);
  }

  vector<ll> V = {0, N - 1};
  for (ll i = 0; i < K; i++)
  {
    ll a = A.at(R.at(i)), b = B.at(R.at(i));
    V.push_back(a), V.push_back(b);
  }
  sort(V.begin(), V.end());
  V.erase(unique(V.begin(), V.end()), V.end());
  ll L = V.size();

  /*
  for (auto v : V)
    cerr << v << " ";
  cerr << endl;
  //*/

  vector<ll> invV(N, -1);
  for (ll i = 0; i < L; i++)
  {
    invV.at(V.at(i)) = i;
  }

  vector<vector<ll>> dists(L);
  for (ll i = 0; i < L; i++)
  {
    dists.at(i) = gr.dijkstra(V.at(i));
    /*
    for (ll v = 0; v < N; v++)
    {
      cerr << dists.at(i).at(v) << " ";
    }
    cerr << endl;
    //*/
  }

  vector dp(L, vector(1LL << K, INF));
  dp.at(0).at(0) = 0;
  for (ll b = 0; b < (1LL << K); b++)
  {
    for (ll i = 0; i < L; i++)
    {
      for (ll j = 0; j < K; j++)
      {
        ll nb = b | (1LL << j);
        ll v1 = A.at(R.at(j)), v2 = B.at(R.at(j));
        ll ni1 = invV.at(v1), ni2 = invV.at(v2);
        ll d2 = dists.at(i).at(v1), d1 = dists.at(i).at(v2);
        ll d = C.at(R.at(j));

        chmin(dp.at(ni1).at(nb), dp.at(i).at(b) + d1 + d);
        chmin(dp.at(ni2).at(nb), dp.at(i).at(b) + d2 + d);

        //cerr << bitset<2>(b) << " " << i << " " << j << "  " << bitset<2>(nb) << " " << v1 << " " << v2 << " " << ni1 << " " << ni2 << " " << d1 << " " << d2 << endl;
      }
    }
  }
  
  ll ans = INF;
  for (ll i = 0; i < L; i++)
  {
    ll tmp = dp.at(i).at((1LL << K) - 1) + dists.at(i).at(N - 1);
    chmin(ans, tmp);
  }
  cout << ans << endl;

  /*
  for (ll b = 0; b < (1LL << K); b++)
  {
    for (ll i = 0; i < L; i++)
    {
      cerr << (bitset<2>)b << " " << i << "  " << dp.at(i).at(b) << endl;
    }
  }
  //*/
}
0