Submission #14069459


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan0
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N, M;
vector<pair<int, int>> E[101010];
int s, K, t[16];
ll dp[1 << 16][16];
//---------------------------------------------------------------------------------------------------
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
int vis[101010];
void dijk(int s, vector<ll>& D) {
    rep(i, 0, N) D[i] = infl;
    rep(i, 0, N) vis[i] = 0;

    min_priority_queue<pair<ll, int>> que;

    D[s] = 0;
    que.push({ 0, s });

    while (!que.empty()) {
        auto q = que.top(); que.pop();

        ll cst = q.first;
        int cu = q.second;

        if (vis[cu]) continue;
        vis[cu] = 1;

        fore(p, E[cu]) {
            ll cst2 = cst + p.second;
            int to = p.first;

            if (chmin(D[to], cst2)) que.push({ D[to], to });
        }
    }
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> M;
    rep(i, 0, M) {
        int a, b; cin >> a >> b;
        a--; b--;
        E[a].push_back({ b, 1 });
        E[b].push_back({ a, 1 });
    }
    cin >> s >> K;
    s--;
    rep(i, 0, K) cin >> t[i], t[i]--;

    vector<ll> fromS(N);
    dijk(s, fromS);

    vector<vector<ll>> fromTs(K, vector<ll>(N));
    rep(k, 0, K) dijk(t[k], fromTs[k]);

    rep(msk, 0, 1 << K) rep(lst, 0, K) dp[msk][lst] = infl;
    rep(k, 0, K) dp[1 << k][k] = fromS[t[k]];
    rep(msk, 0, 1 << K) rep(lst, 0, K) if (dp[msk][lst] < infl) {
        rep(nxt, 0, K) if (!(msk & (1 << nxt))) {
            chmin(dp[msk + (1 << nxt)][nxt], dp[msk][lst] + fromTs[lst][t[nxt]]);
        }
    }

    ll ans = infl;
    rep(lst, 0, K) chmin(ans, dp[(1 << K) - 1][lst]);
    cout << ans << endl;
}



Submission Info

Submission Time
Task M - Real Traveling Salesman
User hamayanhamayan
Language C++ (GCC 9.2.1)
Score 6
Code Size 3065 Byte
Status AC
Exec Time 497 ms
Memory 30648 KiB

Judge Result

Set Name All Sample
Score / Max Score 6 / 6 0 / 0
Status
AC × 37
AC × 2
Set Name Test Cases
All sample_01.txt, sample_02.txt, testcase_1.txt, testcase_10.txt, testcase_11.txt, testcase_12.txt, testcase_13.txt, testcase_14.txt, testcase_15.txt, testcase_16.txt, testcase_17.txt, testcase_18.txt, testcase_19.txt, testcase_2.txt, testcase_20.txt, testcase_21.txt, testcase_22.txt, testcase_23.txt, testcase_24.txt, testcase_25.txt, testcase_26.txt, testcase_27.txt, testcase_28.txt, testcase_29.txt, testcase_3.txt, testcase_30.txt, testcase_31.txt, testcase_32.txt, testcase_33.txt, testcase_34.txt, testcase_35.txt, testcase_4.txt, testcase_5.txt, testcase_6.txt, testcase_7.txt, testcase_8.txt, testcase_9.txt
Sample sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
sample_01.txt AC 5 ms 5952 KiB
sample_02.txt AC 5 ms 5964 KiB
testcase_1.txt AC 261 ms 30648 KiB
testcase_10.txt AC 28 ms 8080 KiB
testcase_11.txt AC 24 ms 8016 KiB
testcase_12.txt AC 4 ms 6100 KiB
testcase_13.txt AC 35 ms 11076 KiB
testcase_14.txt AC 30 ms 9012 KiB
testcase_15.txt AC 25 ms 7944 KiB
testcase_16.txt AC 28 ms 10152 KiB
testcase_17.txt AC 18 ms 7448 KiB
testcase_18.txt AC 26 ms 8024 KiB
testcase_19.txt AC 189 ms 14648 KiB
testcase_2.txt AC 267 ms 29924 KiB
testcase_20.txt AC 295 ms 17292 KiB
testcase_21.txt AC 418 ms 22256 KiB
testcase_22.txt AC 293 ms 17072 KiB
testcase_23.txt AC 415 ms 24564 KiB
testcase_24.txt AC 238 ms 15888 KiB
testcase_25.txt AC 343 ms 19184 KiB
testcase_26.txt AC 241 ms 15896 KiB
testcase_27.txt AC 263 ms 16524 KiB
testcase_28.txt AC 463 ms 25136 KiB
testcase_29.txt AC 116 ms 12396 KiB
testcase_3.txt AC 52 ms 15244 KiB
testcase_30.txt AC 141 ms 12840 KiB
testcase_31.txt AC 497 ms 30004 KiB
testcase_32.txt AC 468 ms 25220 KiB
testcase_33.txt AC 427 ms 22112 KiB
testcase_34.txt AC 5 ms 5912 KiB
testcase_35.txt AC 6 ms 5908 KiB
testcase_4.txt AC 31 ms 10808 KiB
testcase_5.txt AC 22 ms 8244 KiB
testcase_6.txt AC 26 ms 8120 KiB
testcase_7.txt AC 21 ms 7876 KiB
testcase_8.txt AC 14 ms 6904 KiB
testcase_9.txt AC 25 ms 8816 KiB