답안 #852951

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
852951 2023-09-23T08:53:36 Z wakandaaa Cities (BOI16_cities) C++17
100 / 100
2293 ms 49764 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

#define file ""

#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()

#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1LL << (x))
#define popcount __builtin_popcountll

mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
    return l + rd() % (r - l + 1);
}

const int N = 1e5 + 5;
const int mod = (int)1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int oo = 1e9;
const long long ooo = 1e18;

template<class X, class Y> bool mini(X &a, Y b) {
    return a > b ? (a = b, true) : false;
}
template<class X, class Y> bool maxi(X &a, Y b) {
    return a < b ? (a = b, true) : false;
}
void add(int &a, int b) {
    a += b;
    if (a >= mod) a -= mod;
    if (a < 0) a += mod;
}

int n, m, k;
vector<pair<int, int> > adj[N];
int d[N][1 << 5];
vector<int> node;
bool vis[N];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);

    cin >> n >> k >> m;
    node.resize(k);
    for (int i = 0; i < k; ++i) {
        cin >> node[i];
        --node[i];
    }

    for (int i = 0; i < m; ++i) {
        int u, v, c;
        cin >> u >> v >> c;
        --u;
        --v;
        adj[u].emplace_back(v, c);
        adj[v].emplace_back(u, c);
    }

    memset(d, 0x3f, sizeof d);

    long long INF = d[0][0];
    for (int i = 0; i < k; ++i) {
        d[node[i]][1 << i] = 0;
    }

    for (int mask = 1; mask < bit(k); ++mask) {
        for (int x = mask; x; x = (x - 1) & mask) {
            int y = mask ^ x;
            for (int u = 0; u < n; ++u) {
                mini(d[u][mask], d[u][x] + d[u][y]);
            }
        }

        memset(vis, 0, sizeof vis);

        typedef pair<long long, int> node;
        priority_queue<node, vector<node>, greater<node> > q;

        for (int u = 0; u < n; ++u) {
            if (d[u][mask] == INF) continue;
            q.emplace(d[u][mask], u);
        }

        while (q.size()) {
            long long du;
            int u;
            tie(du, u) = q.top(); q.pop();
            if (vis[u])
                continue;
            vis[u] = true;
            for (auto [v, c] : adj[u]) {
                if (mini(d[v][mask], du + c)) 
                    q.emplace(d[v][mask], v);
            }
        }

    }

    long long res = INF;
    for (int u = 0; u < n; ++u) 
        mini(res, d[u][bit(k) - 1]);

    cout << res;

    return 0;
}

/*

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27736 KB Output is correct
2 Correct 5 ms 27740 KB Output is correct
3 Correct 4 ms 27736 KB Output is correct
4 Correct 5 ms 27740 KB Output is correct
5 Correct 4 ms 27740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 527 ms 44712 KB Output is correct
2 Correct 466 ms 48736 KB Output is correct
3 Correct 285 ms 38856 KB Output is correct
4 Correct 51 ms 40460 KB Output is correct
5 Correct 236 ms 47440 KB Output is correct
6 Correct 47 ms 40272 KB Output is correct
7 Correct 6 ms 27996 KB Output is correct
8 Correct 5 ms 27996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 27996 KB Output is correct
2 Correct 8 ms 27996 KB Output is correct
3 Correct 6 ms 27996 KB Output is correct
4 Correct 6 ms 27996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1095 ms 46540 KB Output is correct
2 Correct 965 ms 48980 KB Output is correct
3 Correct 704 ms 38944 KB Output is correct
4 Correct 541 ms 46220 KB Output is correct
5 Correct 123 ms 41648 KB Output is correct
6 Correct 56 ms 42324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2292 ms 46084 KB Output is correct
2 Correct 2212 ms 49132 KB Output is correct
3 Correct 2293 ms 49764 KB Output is correct
4 Correct 1574 ms 39204 KB Output is correct
5 Correct 1047 ms 46204 KB Output is correct
6 Correct 249 ms 41700 KB Output is correct
7 Correct 71 ms 42832 KB Output is correct