結果

問題 No.1812 Uribo Road
ユーザー tokusakuraitokusakurai
提出日時 2022-01-14 22:22:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 5,000 ms
コード長 6,085 bytes
コンパイル時間 2,632 ms
コンパイル使用メモリ 225,032 KB
実行使用メモリ 6,100 KB
最終ジャッジ日時 2023-08-12 20:34:47
合計ジャッジ時間 4,770 ms
ジャッジサーバーID
(参考情報)
judge9 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 43 ms
5,140 KB
testcase_13 AC 20 ms
5,160 KB
testcase_14 AC 18 ms
5,172 KB
testcase_15 AC 33 ms
5,356 KB
testcase_16 AC 27 ms
4,380 KB
testcase_17 AC 86 ms
5,436 KB
testcase_18 AC 19 ms
5,404 KB
testcase_19 AC 104 ms
6,096 KB
testcase_20 AC 104 ms
6,036 KB
testcase_21 AC 82 ms
6,100 KB
testcase_22 AC 64 ms
6,052 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 15 ms
4,988 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 14 ms
4,636 KB
testcase_28 AC 27 ms
5,020 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 54 ms
5,304 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 21 ms
4,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define each(e, v) for (auto &e : v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const int MOD = 1000000007;
// const int MOD = 998244353;

template <typename T, bool directed = false>
struct Weighted_Graph {
    struct edge {
        int to;
        T cost;
        int id;
        edge(int to, T cost, int id) : to(to), cost(cost), id(id) {}
    };

    vector<vector<edge>> es;
    const T INF_T = numeric_limits<T>::max() / 2;
    const int n;
    int m;

    vector<T> d;
    vector<int> pre_v, pre_e;
    vector<int> id;

    Weighted_Graph(int n) : es(n), n(n), m(0), d(n), pre_v(n), pre_e(n) {}

    void add_edge(int from, int to, T cost) {
        es[from].emplace_back(to, cost, m);
        if (!directed) es[to].emplace_back(from, cost, m);
        m++;
    }

    T dijkstra(int s, int t = 0) {
        fill(begin(d), end(d), INF_T);
        using P = pair<T, int>;
        priority_queue<P, vector<P>, greater<P>> que;
        que.emplace(d[s] = 0, s);
        while (!que.empty()) {
            auto [p, i] = que.top();
            que.pop();
            if (p > d[i]) continue;
            for (auto &e : es[i]) {
                // if (id[e.id] != -1) continue;
                if (d[i] + e.cost < d[e.to]) {
                    pre_v[e.to] = i, pre_e[e.to] = e.id;
                    que.emplace(d[e.to] = d[i] + e.cost, e.to);
                }
            }
        }
        return d[t];
    }

    vector<int> shortest_path(int s, int t, bool use_id = false) {
        if (dijkstra(s, t) == INF_T) return {};
        vector<int> ret;
        for (int now = t; now != s; now = pre_v[now]) ret.push_back(use_id ? pre_e[now] : now);
        if (!use_id) ret.push_back(s);
        reverse(begin(ret), end(ret));
        return ret;
    }

    T solve(vector<int> R, vector<int> vs) {
        int K = sz(R);
        id.assign(m, -1);
        rep(i, K) id[R[i]] = i;
        int L = sz(vs);
        vector<int> id2(n, -1);
        rep(i, L) id2[vs[i]] = i;

        vector<vector<T>> dist(L, vector<T>(L));
        rep(i, L) {
            dijkstra(vs[i]);
            rep(j, L) dist[i][j] = d[vs[j]];
        }
        // rep(i, L) print(dist[i]);

        vector<vector<edge>> es2(L);
        rep(i, n) {
            each(e, es[i]) {
                if (id[e.id] != -1) es2[id2[i]].eb(id2[e.to], e.cost, id[e.id]);
            }
        }

        vector<vector<T>> dp(1 << K, vector(L, INF_T));
        dp[0][0] = 0;
        rep(i, 1 << K) {
            rep(j, L) {
                if (dp[i][j] == INF_T) continue;
                rep(k, L) {
                    ll tmp = dp[i][j] + dist[j][k];
                    each(e, es2[k]) {
                        if (flg(i, e.id)) continue;
                        int ni = i | (1 << e.id);
                        int nj = e.to;
                        chmin(dp[ni][nj], tmp + e.cost);
                    }
                }
            }
            // print(dp[i]);
        }

        T ret = INF_T;
        rep(i, L) chmin(ret, dp.back()[i] + dist[i][L - 1]);
        return ret;
    }
};

int main() {
    int N, M, K;
    cin >> N >> M >> K;

    vector<int> R(K);
    rep(i, K) {
        cin >> R[i];
        R[i]--;
    }

    Weighted_Graph<ll> G(N);
    vector<int> vs = {0, N - 1};

    rep(i, M) {
        int u, v;
        ll c;
        cin >> u >> v >> c;
        u--, v--;
        G.add_edge(u, v, c);
        rep(j, K) {
            if (R[j] == i) vs.eb(u), vs.eb(v);
        }
    }
    rearrange(vs);
    // print(vs);

    cout << G.solve(R, vs) << '\n';
}
0