답안 #831027

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
831027 2023-08-19T15:15:56 Z borisAngelov Toll (BOI17_toll) C++17
100 / 100
1500 ms 233568 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 50005;
const int inf = 1000000007;

int n, m, q, k;

vector<pair<int, int>> g[maxn];
vector<pair<int, int>> rev[maxn];

struct query
{
    int l;
    int r;
    int id;
};

query queries[maxn];
int answer[maxn];

int dist[maxn];

void calc(int source, int l, int r)
{
    priority_queue<pair<int, int>> pq;
    pq.push(make_pair(0, source));
    dist[source] = 0;

    while (!pq.empty())
    {
        int node = pq.top().second;
        int curr = -pq.top().first;
        pq.pop();

        for (int i = 0; i < g[node].size(); ++i)
        {
            if (dist[g[node][i].first] > curr + g[node][i].second && g[node][i].first / k <= r)
            {
                dist[g[node][i].first] = curr + g[node][i].second;
                pq.push(make_pair(-dist[g[node][i].first], g[node][i].first));
            }
        }
    }

    while (!pq.empty())
    {
        pq.pop();
    }

    pq.push(make_pair(0, source));
    dist[source] = 0;

    while (!pq.empty())
    {
        int node = pq.top().second;
        int curr = -pq.top().first;
        pq.pop();

        for (int i = 0; i < rev[node].size(); ++i)
        {
            if (dist[rev[node][i].first] > curr + rev[node][i].second && rev[node][i].first / k >= l)
            {
                dist[rev[node][i].first] = curr + rev[node][i].second;
                pq.push(make_pair(-dist[rev[node][i].first], rev[node][i].first));
            }
        }
    }
}

void divide_and_conquer(int l, int r, vector<int> indexes)
{
    if (l >= r || indexes.empty())
    {
        return;
    }

    int mid = (l + r) / 2;

    vector<int> left_part;
    vector<int> right_part;

    for (int node_num = mid * k; node_num < min(n, (mid + 1) * k); ++node_num)
    {
        for (int i = 0; i < n; ++i)
        {
            dist[i] = inf;
        }

        calc(node_num, l, r);

        for (int i = 0; i < indexes.size(); ++i)
        {
            int node_l = queries[indexes[i]].l;
            int node_r = queries[indexes[i]].r;
            int query_idx = queries[indexes[i]].id;

            if (node_l / k <= mid && mid <= node_r / k)
            {
                if (dist[node_l] != inf && dist[node_r] != inf)
                {
                    answer[query_idx] = min(answer[query_idx], dist[node_l] + dist[node_r]);
                }
            }
            else if (node_l / k < mid)
            {
                left_part.push_back(indexes[i]);
            }
            else
            {
                right_part.push_back(indexes[i]);
            }
        }
    }

    divide_and_conquer(l, mid - 1, left_part);
    divide_and_conquer(mid + 1, r, right_part);
}

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main()
{
    fastIO();

    cin >> k >> n >> m >> q;

    for (int i = 1; i <= m; ++i)
    {
        int x, y, w;
        cin >> x >> y >> w;

        g[x].push_back(make_pair(y, w));
        rev[y].push_back(make_pair(x, w));
    }

    vector<int> indexes;

    for (int i = 1; i <= q; ++i)
    {
        cin >> queries[i].l >> queries[i].r;

        queries[i].id = i;
        indexes.push_back(i);
        answer[i] = inf;
    }

    int groups = n / k;

    divide_and_conquer(0, groups, indexes);

    for (int i = 1; i <= q; ++i)
    {
        if (answer[i] == inf)
        {
            cout << "-1\n";
        }
        else
        {
            cout << answer[i] << "\n";
        }
    }

    return 0;
}

Compilation message

toll.cpp: In function 'void calc(int, int, int)':
toll.cpp:37:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |         for (int i = 0; i < g[node].size(); ++i)
      |                         ~~^~~~~~~~~~~~~~~~
toll.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         for (int i = 0; i < rev[node].size(); ++i)
      |                         ~~^~~~~~~~~~~~~~~~~~
toll.cpp: In function 'void divide_and_conquer(int, int, std::vector<int>)':
toll.cpp:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         for (int i = 0; i < indexes.size(); ++i)
      |                         ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 6356 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 26 ms 7124 KB Output is correct
9 Correct 20 ms 6996 KB Output is correct
10 Correct 7 ms 3284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 7244 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 3 ms 3176 KB Output is correct
8 Correct 5 ms 3540 KB Output is correct
9 Correct 17 ms 7124 KB Output is correct
10 Correct 153 ms 40916 KB Output is correct
11 Correct 43 ms 8684 KB Output is correct
12 Correct 119 ms 30808 KB Output is correct
13 Correct 1500 ms 233568 KB Output is correct
14 Correct 62 ms 12100 KB Output is correct
15 Correct 457 ms 70320 KB Output is correct
16 Correct 1232 ms 191608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2676 KB Output is correct
5 Correct 2 ms 2660 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 5 ms 2948 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 16 ms 6740 KB Output is correct
11 Correct 50 ms 7432 KB Output is correct
12 Correct 100 ms 10088 KB Output is correct
13 Correct 98 ms 10572 KB Output is correct
14 Correct 85 ms 8860 KB Output is correct
15 Correct 72 ms 6636 KB Output is correct
16 Correct 89 ms 7576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2676 KB Output is correct
5 Correct 2 ms 2660 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 5 ms 2948 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 16 ms 6740 KB Output is correct
11 Correct 50 ms 7432 KB Output is correct
12 Correct 100 ms 10088 KB Output is correct
13 Correct 98 ms 10572 KB Output is correct
14 Correct 85 ms 8860 KB Output is correct
15 Correct 72 ms 6636 KB Output is correct
16 Correct 89 ms 7576 KB Output is correct
17 Correct 62 ms 7724 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2816 KB Output is correct
24 Correct 4 ms 2940 KB Output is correct
25 Correct 43 ms 4560 KB Output is correct
26 Correct 15 ms 3448 KB Output is correct
27 Correct 21 ms 6924 KB Output is correct
28 Correct 167 ms 12748 KB Output is correct
29 Correct 177 ms 13372 KB Output is correct
30 Correct 114 ms 9424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 6356 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 26 ms 7124 KB Output is correct
9 Correct 20 ms 6996 KB Output is correct
10 Correct 7 ms 3284 KB Output is correct
11 Correct 38 ms 7244 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 3 ms 3176 KB Output is correct
18 Correct 5 ms 3540 KB Output is correct
19 Correct 17 ms 7124 KB Output is correct
20 Correct 153 ms 40916 KB Output is correct
21 Correct 43 ms 8684 KB Output is correct
22 Correct 119 ms 30808 KB Output is correct
23 Correct 1500 ms 233568 KB Output is correct
24 Correct 62 ms 12100 KB Output is correct
25 Correct 457 ms 70320 KB Output is correct
26 Correct 1232 ms 191608 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2676 KB Output is correct
31 Correct 2 ms 2660 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2688 KB Output is correct
34 Correct 5 ms 2948 KB Output is correct
35 Correct 4 ms 2772 KB Output is correct
36 Correct 16 ms 6740 KB Output is correct
37 Correct 50 ms 7432 KB Output is correct
38 Correct 100 ms 10088 KB Output is correct
39 Correct 98 ms 10572 KB Output is correct
40 Correct 85 ms 8860 KB Output is correct
41 Correct 72 ms 6636 KB Output is correct
42 Correct 89 ms 7576 KB Output is correct
43 Correct 62 ms 7724 KB Output is correct
44 Correct 1 ms 2644 KB Output is correct
45 Correct 1 ms 2644 KB Output is correct
46 Correct 1 ms 2644 KB Output is correct
47 Correct 1 ms 2644 KB Output is correct
48 Correct 1 ms 2644 KB Output is correct
49 Correct 2 ms 2816 KB Output is correct
50 Correct 4 ms 2940 KB Output is correct
51 Correct 43 ms 4560 KB Output is correct
52 Correct 15 ms 3448 KB Output is correct
53 Correct 21 ms 6924 KB Output is correct
54 Correct 167 ms 12748 KB Output is correct
55 Correct 177 ms 13372 KB Output is correct
56 Correct 114 ms 9424 KB Output is correct
57 Correct 1364 ms 64868 KB Output is correct
58 Correct 25 ms 7128 KB Output is correct
59 Correct 84 ms 8308 KB Output is correct