結果

問題 No.1812 Uribo Road
ユーザー asaringoasaringo
提出日時 2022-01-15 20:12:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 5,000 ms
コード長 3,576 bytes
コンパイル時間 2,008 ms
コンパイル使用メモリ 195,736 KB
実行使用メモリ 27,188 KB
最終ジャッジ日時 2023-08-14 05:38:14
合計ジャッジ時間 5,023 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
17,120 KB
testcase_01 AC 8 ms
17,120 KB
testcase_02 AC 8 ms
17,288 KB
testcase_03 AC 62 ms
25,648 KB
testcase_04 AC 8 ms
17,444 KB
testcase_05 AC 8 ms
17,408 KB
testcase_06 AC 8 ms
17,140 KB
testcase_07 AC 12 ms
18,016 KB
testcase_08 AC 11 ms
17,520 KB
testcase_09 AC 12 ms
17,604 KB
testcase_10 AC 10 ms
17,864 KB
testcase_11 AC 9 ms
17,384 KB
testcase_12 AC 100 ms
26,332 KB
testcase_13 AC 83 ms
26,352 KB
testcase_14 AC 83 ms
26,272 KB
testcase_15 AC 45 ms
18,776 KB
testcase_16 AC 45 ms
19,516 KB
testcase_17 AC 119 ms
22,416 KB
testcase_18 AC 89 ms
26,852 KB
testcase_19 AC 165 ms
27,188 KB
testcase_20 AC 172 ms
27,008 KB
testcase_21 AC 144 ms
27,084 KB
testcase_22 AC 130 ms
27,144 KB
testcase_23 AC 19 ms
18,980 KB
testcase_24 AC 8 ms
17,152 KB
testcase_25 AC 23 ms
18,380 KB
testcase_26 AC 9 ms
17,344 KB
testcase_27 AC 47 ms
22,000 KB
testcase_28 AC 34 ms
18,548 KB
testcase_29 AC 8 ms
17,316 KB
testcase_30 AC 12 ms
17,696 KB
testcase_31 AC 120 ms
26,620 KB
testcase_32 AC 12 ms
17,932 KB
testcase_33 AC 89 ms
26,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef long double ld ;
typedef pair<ll,ll> P ;
typedef tuple<ll,ll,ll> TP ;
#define chmin(a,b) a = min(a,b)
#define chmax(a,b) a = max(a,b)
#define bit_count(x) __builtin_popcountll(x)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a / gcd(a,b) * b
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define rrep(i,a,b) for(int i = a ; i < b ; i++)
#define endl "\n"

struct edge{
    int to ;
    ll cost ;
};

int n , m , k ;
int R[20] ;
ll d[30][10101] ;
vector<edge> G[10101] ;
unordered_map<int,int> encr , decr ;
unordered_map<int,ll> dp[1<<13][13] ;
unordered_map<int,bool> visited[1<<13][13] ;
TP E[20202] ;

void djikstra(int s){
    rep(i,n) d[s][i] = 1e16 ;
    d[s][decr[s]] = 0 ;
    priority_queue<P,vector<P>,greater<P>> que ;
    que.push(P(0,decr[s])) ;
    while(!que.empty()){
        P p = que.top() ; que.pop() ;
        int v = p.second ;
        if(d[s][v] < p.first) continue;
        for(int i = 0 ; i < G[v].size() ; i++){
            edge e = G[v][i] ;
            if(d[s][e.to] > d[s][v] + e.cost){
                d[s][e.to] = d[s][v] + e.cost ;
                que.push(P(d[s][e.to],e.to)) ;
            }
        }
    }
}

int main(){
    cin >> n >> m >> k ;
    rep(i,k) cin >> R[i] , R[i]-- ;
    rep(i,m){
        int a , b , c ;
        cin >> a >> b >> c ;
        a-- ; b-- ;
        G[a].push_back(edge{b,c}) ;
        G[b].push_back(edge{a,c}) ;
        E[i] = {a,b,c} ;
    }
    int id = 0 ;
    rep(i,k){
        int r = R[i] ;
        TP p = E[r] ;
        ll a , b , c ;
        tie(a,b,c) = p ;
        encr[a] = id ; decr[id] = a ;
        djikstra(id) ; id++ ;
        encr[b] = id ; decr[id] = b ;
        djikstra(id) ; id++ ;
    }
    rep(i,k){
        int r = R[i] ;
        TP p = E[r] ;
        ll a , b , c ;
        tie(a,b,c) = p ;
        dp[1 << i][i][a] = d[encr[b]][0] + c ;
        dp[1 << i][i][b] = d[encr[a]][0] + c ;
        visited[1 << i][i][a] = true ;
        visited[1 << i][i][b] = true ;
    }
    rep(S,1<<k){
        rep(i,k){
            if(!(S >> i & 1)) continue ;
            rep(j,k){
                if(S >> j & 1) continue ;
                int r = R[j] ;
                TP p = E[r] ;
                ll a , b , c ;
                tie(a,b,c) = p ;
                for(auto it : dp[S][i]){
                    ll node = it.first , val = it.second ;
                    if(!visited[S | 1 << j][j][a]){
                        dp[S | 1 << j][j][a] = val + d[encr[node]][b] + c ;
                        visited[S | 1 << j][j][a] = true ;
                    }
                    if(!visited[S | 1 << j][j][b]){
                        dp[S | 1 << j][j][b] = val + d[encr[node]][a] + c ;
                        visited[S | 1 << j][j][b] = true ;
                    }
                }
                for(auto it : dp[S][i]){
                    ll node = it.first , val = it.second ;
                    if(dp[S | 1 << j][j][a] > val + d[encr[node]][b] + c){
                        dp[S | 1 << j][j][a] = val + d[encr[node]][b] + c ;
                    }
                    if(dp[S | 1 << j][j][b] > val + d[encr[node]][a] + c){
                        dp[S | 1 << j][j][b] = val + d[encr[node]][a] + c ;
                    }
                }
            }
        }
    }
    ll res = 1e16 ;
    rep(i,k){
        for(auto it : dp[(1<<k)-1][i]){
            ll node = it.first , val = it.second ;
            if(visited[(1<<k)-1][i][node]) chmin(res,val+d[encr[node]][n-1]) ;
        }
    }
    cout << res << endl ;
}
0