답안 #26249

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
26249 2017-06-28T14:40:15 Z sgtlaugh Cities (BOI16_cities) C++14
100 / 100
3816 ms 74676 KB
#include <stdio.h>
#include <bits/stdtr1c++.h>

#define MAXK 5
#define MAXN 100010
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define dbg(x) cout << #x << " = " << x << endl
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))

using namespace std;

const long long INF = 1LL << 60;

bitset <MAXN> visited;
long long dis[1 << MAXK][MAXN];
vector <pair<int, int> > graph[MAXN];
int n, k, m, Q[10000000], special[MAXK];

void spfa(long long dis[MAXN]){
    int i, j, x, w, f = 0, l = 0;
    for (i = 0; i <= n; i++){
        visited[i] = 0;
        if (dis[i] < INF) Q[l++] = i;
    }

    while (f < l){
        i = Q[f++];
        for (j = 0; j < graph[i].size(); j++){
            x = graph[i][j].first, w = graph[i][j].second;
            if (dis[x] > dis[i] + w){
                dis[x] = dis[i] + w;
                if (!visited[x]){
                    visited[x] = 1;
                    if (f && rand() & 7) Q[--f] = x;
                    else Q[l++] = x;
                }
            }
        }
        visited[i] = 0;
    }
}

long long minimumSteinerTree(){
    int i, j, mask, submask;

    for (mask = 1; mask < (1 << k); mask++){
        for (i = 1; i <= n; i++) dis[mask][i] = INF;
        if (__builtin_popcount(mask) == 1){
            dis[mask][special[31 - __builtin_clz(mask)]] = 0;
        }
        else{
            for (i = 1; i <= n; i++){
                for (submask = mask - 1; submask >= (submask ^ mask); submask = (submask - 1) & mask){
                    dis[mask][i] = min(dis[mask][i], dis[submask][i] + dis[submask ^ mask][i]);
                }
            }
        }
        spfa(dis[mask]);
    }

    return *std::min_element(dis[mask - 1] + 1, dis[mask - 1] + 1 + n);
}

int main(){
    int i, j, u, v, w;

    while (scanf("%d %d %d", &n, &k, &m) != EOF){
        for (i = 0; i < MAXN; i++) graph[i].clear();
        for (i = 0; i < k; i++) scanf("%d", &special[i]);

        for (i = 0; i < m; i++){
            scanf("%d %d %d", &u, &v, &w);
            graph[u].push_back(make_pair(v, w));
            graph[v].push_back(make_pair(u, w));
        }

        printf("%lld\n", minimumSteinerTree());
    }
    return 0;
}

Compilation message

cities.cpp: In function 'void spfa(long long int*)':
cities.cpp:29:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < graph[i].size(); j++){
                       ^
cities.cpp: In function 'long long int minimumSteinerTree()':
cities.cpp:45:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j, mask, submask;
            ^
cities.cpp: In function 'int main()':
cities.cpp:66:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j, u, v, w;
            ^
cities.cpp:70:57: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         for (i = 0; i < k; i++) scanf("%d", &special[i]);
                                                         ^
cities.cpp:73:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d %d", &u, &v, &w);
                                          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 68444 KB Output is correct
2 Correct 0 ms 68444 KB Output is correct
3 Correct 0 ms 68444 KB Output is correct
4 Correct 0 ms 68444 KB Output is correct
5 Correct 0 ms 68444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 786 ms 74648 KB Output is correct
2 Correct 503 ms 74676 KB Output is correct
3 Correct 2256 ms 71612 KB Output is correct
4 Correct 116 ms 72800 KB Output is correct
5 Correct 396 ms 74648 KB Output is correct
6 Correct 123 ms 72800 KB Output is correct
7 Correct 0 ms 68576 KB Output is correct
8 Correct 0 ms 68576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 68576 KB Output is correct
2 Correct 3 ms 68576 KB Output is correct
3 Correct 3 ms 68444 KB Output is correct
4 Correct 3 ms 68576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 906 ms 74648 KB Output is correct
2 Correct 816 ms 74436 KB Output is correct
3 Correct 2556 ms 71612 KB Output is correct
4 Correct 566 ms 74252 KB Output is correct
5 Correct 243 ms 73636 KB Output is correct
6 Correct 163 ms 74600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1929 ms 74648 KB Output is correct
2 Correct 2199 ms 74648 KB Output is correct
3 Correct 1043 ms 74512 KB Output is correct
4 Correct 3816 ms 71612 KB Output is correct
5 Correct 833 ms 74252 KB Output is correct
6 Correct 376 ms 73560 KB Output is correct
7 Correct 189 ms 74600 KB Output is correct