結果

問題 No.1812 Uribo Road
ユーザー milanis48663220milanis48663220
提出日時 2022-01-14 23:36:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 154 ms / 5,000 ms
コード長 3,545 bytes
コンパイル時間 1,678 ms
コンパイル使用メモリ 138,096 KB
最終ジャッジ日時 2025-01-27 12:25:45
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

const ll INF = 1e+15;

typedef pair<ll, int> P;

struct edge{
    int to;
    ll cost;
};

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

using T = tuple<ll, int, int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m, k; cin >> n >> m >> k;
    vector<int> t(k);
    for(int i = 0; i < k; i++){
        cin >> t[i]; t[i]--;
    }
    vector<vector<edge>> g(n+2*k);
    vector<int> a(m), b(m), c(m);
    for(int i = 0; i < m; i++) {
        cin >> a[i] >> b[i] >> c[i]; a[i]--; b[i]--;
        g[a[i]].push_back(edge{b[i], c[i]});
        g[b[i]].push_back(edge{a[i], c[i]});
    }
    vector<int> r = {0};
    vector<int> idx = {-1};
    int cur = n;
    for(int j = 0; j < k; j++){
        r.push_back(cur);
        idx.push_back(j);
        int i = t[j];
        g[a[i]].push_back(edge{cur, 0});
        g[cur].push_back(edge{b[i], c[i]});
        cur++;
        r.push_back(cur);
        idx.push_back(j);
        g[b[i]].push_back(edge{cur, 0});
        g[cur].push_back(edge{a[i], c[i]});
        cur++;
    }
    r.push_back(n-1);
    idx.push_back(-1);
    auto h = vec2d(2*k+2, 2*k+2, 0ll);
    for(int i = 0; i <= 2*k+1; i++){
        auto d = dijkstra(r[i], g);
        for(int j = 0; j <= 2*k+1; j++){
            h[i][j] = d[r[j]];
        }
    }
    auto dist = vec2d(2*k+2, 1<<k, INF);
    priority_queue<T, vector<T>, greater<T>> que;
    auto set_dist = [&](int v, int s, ll d){
        if(chmin(dist[v][s], d)){
            que.push(T(d, v, s));
        }
    };

    set_dist(0, 0, 0);
    while(!que.empty()){
        auto [d, v, s] = que.top(); que.pop();
        for(int i = 0; i <= 2*k+1; i++){
            int t = s;
            if(idx[i] != -1) t |= (1<<idx[i]);
            set_dist(i, t, d + h[v][i]);
        }
    }
    cout << dist[2*k+1][(1<<k)-1] << endl;
}
0