結果

問題 No.1812 Uribo Road
ユーザー rogi52rogi52
提出日時 2022-10-22 07:02:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 5,000 ms
コード長 2,468 bytes
コンパイル時間 2,632 ms
コンパイル使用メモリ 223,516 KB
実行使用メモリ 7,756 KB
最終ジャッジ日時 2023-09-14 04:14:35
合計ジャッジ時間 5,349 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 38 ms
6,860 KB
testcase_13 AC 16 ms
6,908 KB
testcase_14 AC 16 ms
6,848 KB
testcase_15 AC 26 ms
4,960 KB
testcase_16 AC 23 ms
5,568 KB
testcase_17 AC 69 ms
6,916 KB
testcase_18 AC 12 ms
5,340 KB
testcase_19 AC 82 ms
7,756 KB
testcase_20 AC 85 ms
7,700 KB
testcase_21 AC 67 ms
7,752 KB
testcase_22 AC 50 ms
7,756 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 12 ms
4,608 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 10 ms
4,384 KB
testcase_28 AC 22 ms
5,076 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 40 ms
5,896 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 26 ms
5,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;

// g <- pair < v , cost > 
template < class T >
vector< T > dijkstra(vector<vector<pair<int, T>>> &graph, int s) {
    T INF = numeric_limits< T >::max();
    vector<T> dist(graph.size(), INF);
    priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;
    q.push({dist[s] = T(0), s});
    while(!q.empty()){
        auto [uc, ui] = q.top(); q.pop();
        if(uc != dist[ui]) continue;
        for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) 
            q.push({dist[vi] = uc + vc, vi});
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,M,K; cin >> N >> M >> K;
    vector<int> R(K);
    rep(i,K) cin >> R[i], R[i]--;
    vector<vector<pair<int,ll>>> G(N);
    vector<pair<pair<int,int>,ll>> E(M);
    for(auto &[e, c] : E) {
        auto &[u, v] = e;
        cin >> u >> v >> c; u--; v--;
        G[u].push_back({v, c});
        G[v].push_back({u, c});
    }

    vector<vector<pair<ll,ll>>> dp(1 << K, vector<pair<ll,ll>>(K, {1e18, 1e18}));
    vector<pair<vector<ll>,vector<ll>>> dist(K);
    auto chmin = [&](ll &a, ll b){ if(a > b) a = b; };
    rep(i,K) {
        dist[i].first  = dijkstra(G, E[R[i]].first.first );
        dist[i].second = dijkstra(G, E[R[i]].first.second);
    }
    vector<ll> dist0 = dijkstra(G, 0);
    rep(i,K) {
        chmin(dp[(1 << i)][i].first , dist0[E[R[i]].first.second] + E[R[i]].second);
        chmin(dp[(1 << i)][i].second, dist0[E[R[i]].first.first ] + E[R[i]].second);
    }

    rep(S,1<<K) {
        rep(u,K) if(S & (1 << u)) {
            rep(v,K) if(!(S & (1 << v))) {
                chmin(dp[S | (1 << v)][v].first , dp[S][u].first  + dist[u].first [E[R[v]].first.second] + E[R[v]].second);
                chmin(dp[S | (1 << v)][v].first , dp[S][u].second + dist[u].second[E[R[v]].first.second] + E[R[v]].second);
                chmin(dp[S | (1 << v)][v].second, dp[S][u].first  + dist[u].first [E[R[v]].first.first ] + E[R[v]].second);
                chmin(dp[S | (1 << v)][v].second, dp[S][u].second + dist[u].second[E[R[v]].first.first ] + E[R[v]].second);
            }
        }
    }
    ll ans = 1e18;
    rep(i,K) {
        chmin(ans, dp[(1 << K) - 1][i].first  + dist[i].first [N - 1]);
        chmin(ans, dp[(1 << K) - 1][i].second + dist[i].second[N - 1]);
    }
    cout << ans << "\n";
}
0