답안 #780111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
780111 2023-07-12T06:42:56 Z adaawf 공장들 (JOI14_factories) C++17
33 / 100
8000 ms 148736 KB
#include "factories.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
using namespace std;
long long int f[500005];
int n, d[500005];
vector<int> gg[500005], g[500005];
int p[500005][25];
void bfs(int x) {
    queue<int> q;
    q.push(x);
    p[x][0] = x;
    f[x] = d[x] = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int j = 1; j <= 19; j++) {
             p[u][j] = p[p[u][j - 1]][j - 1];
             if (p[u][j] == 0) break;
        }
        for (int i = 0; i < g[u].size(); i++) {
            int v = g[u][i];
            if (d[u] + 1 < d[v]) {
                d[v] = d[u] + 1;
                f[v] = f[u] + gg[u][i];
                p[v][0] = u;
                q.push(v);
            }
        }
    }
}
int lca(int u, int v) {
    if (d[u] > d[v]) {
        swap(u, v);
    }
    for (int j = 19; j >= 0; j--) {
        if (d[p[v][j]] >= d[u]) {
            v = p[v][j];
        }
    }
    if (u == v) return u;
    for (int j = 19; j >= 0; j--) {
        if (p[u][j] != p[v][j]) {
            u = p[u][j];
            v = p[v][j];
        }
    }
    return p[v][0];
}
long long int check(int u, int v) {
    return f[u] + f[v] - f[lca(u, v)] * 2;
}
void Init(int N, int A[], int B[], int D[]) {
    n = N;
    for (int i = 0; i < 500005; i++) {
        f[i] = 999999999999999999;
        d[i] = n * 2;
    }
    for (int i = 0; i < n - 1; i++) {
        int u = A[i], v = B[i], w = D[i];
        g[u].push_back(v);
        g[v].push_back(u);
        gg[u].push_back(w);
        gg[v].push_back(w);
    }
    bfs(0);
}
long long int Query(int S, int X[], int T, int Y[]) {
    if (S > 45 && T > 45) {
        vector<long long int> ff(n);
        vector<bool> dda(n), dd(n);
        for (int i = 0; i <= n; i++) {
            dd[i] = dda[i] = 0;
            ff[i] = 999999999999999999;
        }
        for (int i = 0; i < T; i++) {
            dda[Y[i]] = 1;
        }
        priority_queue<pair<long long int, long long int>> q;
        for (int i = 0; i < S; i++) {
            q.push({0, X[i]});
            ff[X[i]] = 0;
        }
        while (!q.empty()) {
            int u = q.top().second;
            q.pop();
            if (dd[u] == 1) continue;
            dd[u] = 1;
            if (dda[u] == 1) return ff[u];
            for (int i = 0; i < g[u].size(); i++) {
                int v = g[u][i], w = gg[u][i];
                if (ff[u] + w < ff[v]) {
                    ff[v] = ff[u] + w;
                    q.push({-ff[v], v});
                }
            }
        }
    }
    long long int res = 999999999999999999;
    for (int i = 0; i < S; i++) {
        for (int j = 0; j < T; j++) {
            res = min(res, check(X[i], Y[j]));
        }
    }
    return res;
}

Compilation message

factories.cpp: In function 'void bfs(int)':
factories.cpp:22:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |         for (int i = 0; i < g[u].size(); i++) {
      |                         ~~^~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:91:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |             for (int i = 0; i < g[u].size(); i++) {
      |                             ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 29908 KB Output is correct
2 Correct 491 ms 38684 KB Output is correct
3 Correct 340 ms 39268 KB Output is correct
4 Correct 954 ms 39560 KB Output is correct
5 Correct 443 ms 39348 KB Output is correct
6 Correct 599 ms 39628 KB Output is correct
7 Correct 362 ms 39292 KB Output is correct
8 Correct 757 ms 39112 KB Output is correct
9 Correct 456 ms 39064 KB Output is correct
10 Correct 665 ms 39320 KB Output is correct
11 Correct 354 ms 39024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 29780 KB Output is correct
2 Correct 1746 ms 124020 KB Output is correct
3 Correct 3798 ms 121224 KB Output is correct
4 Correct 1117 ms 126716 KB Output is correct
5 Correct 4329 ms 122880 KB Output is correct
6 Correct 2783 ms 123108 KB Output is correct
7 Correct 5780 ms 56164 KB Output is correct
8 Correct 1783 ms 58400 KB Output is correct
9 Correct 4173 ms 57460 KB Output is correct
10 Correct 3467 ms 57452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 29908 KB Output is correct
2 Correct 491 ms 38684 KB Output is correct
3 Correct 340 ms 39268 KB Output is correct
4 Correct 954 ms 39560 KB Output is correct
5 Correct 443 ms 39348 KB Output is correct
6 Correct 599 ms 39628 KB Output is correct
7 Correct 362 ms 39292 KB Output is correct
8 Correct 757 ms 39112 KB Output is correct
9 Correct 456 ms 39064 KB Output is correct
10 Correct 665 ms 39320 KB Output is correct
11 Correct 354 ms 39024 KB Output is correct
12 Correct 19 ms 29780 KB Output is correct
13 Correct 1746 ms 124020 KB Output is correct
14 Correct 3798 ms 121224 KB Output is correct
15 Correct 1117 ms 126716 KB Output is correct
16 Correct 4329 ms 122880 KB Output is correct
17 Correct 2783 ms 123108 KB Output is correct
18 Correct 5780 ms 56164 KB Output is correct
19 Correct 1783 ms 58400 KB Output is correct
20 Correct 4173 ms 57460 KB Output is correct
21 Correct 3467 ms 57452 KB Output is correct
22 Correct 1246 ms 132588 KB Output is correct
23 Correct 2032 ms 136180 KB Output is correct
24 Correct 1340 ms 131656 KB Output is correct
25 Correct 1542 ms 132696 KB Output is correct
26 Correct 2582 ms 128012 KB Output is correct
27 Correct 2024 ms 132164 KB Output is correct
28 Correct 1496 ms 148736 KB Output is correct
29 Correct 6602 ms 128112 KB Output is correct
30 Correct 7671 ms 128116 KB Output is correct
31 Execution timed out 8064 ms 128036 KB Time limit exceeded
32 Halted 0 ms 0 KB -