Submission #202284

# Submission time Handle Problem Language Result Execution time Memory
202284 2020-02-15T06:30:47 Z PeppaPig Cities (BOI16_cities) C++14
22 / 100
4821 ms 113984 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 <= 5; 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(id[v.x] != -1) cost[v.x][bit + (1 << id[v.x])] = c + v.y;
                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 20 ms 28152 KB Output is correct
2 Correct 20 ms 28024 KB Output is correct
3 Correct 19 ms 28152 KB Output is correct
4 Correct 21 ms 28064 KB Output is correct
5 Correct 19 ms 28152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1052 ms 58568 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 28580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2109 ms 64768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4821 ms 113984 KB Output isn't correct
2 Halted 0 ms 0 KB -