Submission #202285

# Submission time Handle Problem Language Result Execution time Memory
202285 2020-02-15T06:32:23 Z PeppaPig Cities (BOI16_cities) C++14
100 / 100
4801 ms 116884 KB
#include <bits/stdc++.h>

#define long long long
#define pii pair<long, long>
#define x first
#define y second

using namespace std;

const int N = 1e5+5;

int n, k, m, id[N];
long cost[N][1 << 5];
vector<pii> g[N];
vector<int> vbit[6];

int main() {
    memset(id, -1, sizeof id);
    fill_n(cost[0], N * (1 << 5), 1e18);

    scanf("%d %d %d", &n, &k, &m);
    for(int i = 0, a; i < k; i++) {
        scanf("%d", &a);
        id[a - 1] = i;
    }
    for(int i = 1, a, b, c; i <= m; i++) {
        scanf("%d %d %d", &a, &b, &c);
        g[a-1].emplace_back(b-1, c);
        g[b-1].emplace_back(a-1, c);
    }

    for(int i = 1; i < (1 << k); i++)
        vbit[__builtin_popcount(i)].emplace_back(i);

    for(int step = 1; step <= k; step++) {
        priority_queue<pair<long, pii> > Q;
        if(step == 1) {
            for(int i = 0; i < n; i++) if(id[i] != -1) {
                cost[i][1 << id[i]] = 0;
                Q.emplace(-cost[i][1 << id[i]], make_pair(i, 1ll << id[i]));
            } 
        } else {
            for(int i = 0; i < n; i++) for(int x : vbit[step]) {
                for(int y = 0; y < (1 << k); y++) {
                    if((x & y) != y) continue;
                    cost[i][x] = min(cost[i][x], cost[i][y] + cost[i][x ^ y]);
                }
                Q.emplace(-cost[i][x], make_pair(i, x));
            }
        }
        while(!Q.empty()) {
            auto now = Q.top(); Q.pop();
            if(cost[now.y.x][now.y.y] != -now.x) continue;

            long c = -now.x;
            int u = now.y.x, bit = now.y.y;
            for(pii v : g[u]) if(cost[v.x][bit] > c + v.y) {
                cost[v.x][bit] = c + v.y;
                Q.emplace(-cost[v.x][bit], make_pair(v.x, bit));
            }
        }
    }

    long ans = 1e18;
    for(int i = 0; i < n; i++) ans = min(ans, cost[i][(1 << k) - 1]);
    printf("%lld\n", ans);

    return 0;
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &n, &k, &m);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:23:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a);
         ~~~~~^~~~~~~~~~
cities.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &a, &b, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 28152 KB Output is correct
2 Correct 22 ms 28152 KB Output is correct
3 Correct 21 ms 28152 KB Output is correct
4 Correct 21 ms 28152 KB Output is correct
5 Correct 20 ms 28152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 976 ms 57136 KB Output is correct
2 Correct 960 ms 60356 KB Output is correct
3 Correct 551 ms 47960 KB Output is correct
4 Correct 122 ms 40900 KB Output is correct
5 Correct 475 ms 51928 KB Output is correct
6 Correct 121 ms 40900 KB Output is correct
7 Correct 24 ms 28408 KB Output is correct
8 Correct 22 ms 28408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 28580 KB Output is correct
2 Correct 26 ms 28580 KB Output is correct
3 Correct 25 ms 28472 KB Output is correct
4 Correct 24 ms 28408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2068 ms 63272 KB Output is correct
2 Correct 2044 ms 81356 KB Output is correct
3 Correct 1343 ms 60232 KB Output is correct
4 Correct 1179 ms 61920 KB Output is correct
5 Correct 294 ms 45876 KB Output is correct
6 Correct 130 ms 43088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4679 ms 112656 KB Output is correct
2 Correct 4753 ms 116884 KB Output is correct
3 Correct 4801 ms 115768 KB Output is correct
4 Correct 3074 ms 83776 KB Output is correct
5 Correct 2509 ms 82524 KB Output is correct
6 Correct 477 ms 50248 KB Output is correct
7 Correct 155 ms 43404 KB Output is correct