Submission #876902

# Submission time Handle Problem Language Result Execution time Memory
876902 2023-11-22T13:22:56 Z andrei_iorgulescu Toll (BOI17_toll) C++14
100 / 100
439 ms 12216 KB
#include <bits/stdc++.h>

using namespace std;

int inf = 1e9;

int k,n,m,q,t;
vector<pair<int,int>>g[50005],gr[50005];
int ans[10005];
int dist[50005];

struct ura
{
    int x,y,idx;
};

void dijkstral(int start,int leftbound)
{
    priority_queue<pair<int,int>>pq;
    pq.push({0,start});
    while (!pq.empty())
    {
        int timp = -pq.top().first,nod = pq.top().second;
        pq.pop();
        if (dist[nod] == inf)
        {
            dist[nod] = timp;
            for (auto vecin : gr[nod])
            {
                if (vecin.first >= leftbound)
                    pq.push({-(vecin.second + timp),vecin.first});
            }
        }
    }
}

void dijkstrar(int start,int rightbound)
{
    priority_queue<pair<int,int>>pq;
    pq.push({0,start});
    while (!pq.empty())
    {
        int timp = -pq.top().first,nod = pq.top().second;
        pq.pop();
        if (dist[nod] == inf)
        {
            dist[nod] = timp;
            for (auto vecin : g[nod])
            {
                if (vecin.first <= rightbound)
                    pq.push({-(vecin.second + timp),vecin.first});
            }
        }
    }
}

void solve(int l,int r,vector<ura>&v)
{
    if (l == r)
        return;
    int mij = (l + r) / 2;
    ///pentru query-uri cu st <= mij si dr > mij, sigur trec prin ceva din mij
    ///incerc fiecare din cele k valori ale mij ca fiind "punctul de trecere"
    ///valorile de mijloc sunt mij * k, mij * k + 1,...mij * k + k - 1
    vector<ura>v1,v2,s;
    for (auto it : v)
    {
        if (it.y / k <= mij)
            v1.push_back(it);
        else if (it.x / k > mij)
            v2.push_back(it);
        else
            s.push_back(it);
    }
    if (v1.size() != 0)
        solve(l,mij,v1);
    if (v2.size() != 0)
        solve(mij + 1,r,v2);
    for (int i = mij * k; i < min((mij + 1) * k,n + 1); i++)
    {
        ///i este nodul de trecere
        ///fac un dijkstra in stanga si unul in dreapta din nodul i, bounded by [l * k, r * (k + 1) - 1] si incerc sa rezolv fiecare query din s
        for (int j = l * k; j < min(n + 1,(r + 1) * k); j++)
            dist[j] = inf;
        dijkstral(i,l * k);
        dist[i] = inf;
        dijkstrar(i,(r + 1) * k - 1);
        for (auto it : s)
        {
            ans[it.idx] = min(ans[it.idx],dist[it.x] + dist[it.y]);
            //cout << "Q " << dist[it.x] << ' ' << dist[it.y] << '\n';
        }
        for (int j = l * k; j < min(n + 1,(r + 1) * k); j++)
            dist[j] = inf;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> k >> n >> m >> q;
    t = (n - 1) / k;
    for (int i = 1; i <= m; i++)
    {
        int x,y,z;
        cin >> x >> y >> z;
        g[x].push_back({y,z});
        gr[y].push_back({x,z});
    }
    vector<ura>v;
    for (int i = 1; i <= q; i++)
    {
        ura aux;
        cin >> aux.x >> aux.y;
        aux.idx = i;
        v.push_back(aux);
    }
    for (int i = 1; i <= q; i++)
        ans[i] = inf;
    solve(0,t,v);
    for (int i = 1; i <= q; i++)
    {
        if (ans[i] == inf)
            cout << -1 << '\n';
        else
            cout << ans[i] << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 30 ms 7384 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2872 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2972 KB Output is correct
7 Correct 2 ms 2964 KB Output is correct
8 Correct 28 ms 7304 KB Output is correct
9 Correct 17 ms 7228 KB Output is correct
10 Correct 4 ms 3356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 8216 KB Output is correct
2 Correct 1 ms 2872 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 4 ms 3360 KB Output is correct
8 Correct 4 ms 3356 KB Output is correct
9 Correct 19 ms 7452 KB Output is correct
10 Correct 107 ms 10532 KB Output is correct
11 Correct 46 ms 8212 KB Output is correct
12 Correct 32 ms 7680 KB Output is correct
13 Correct 149 ms 10816 KB Output is correct
14 Correct 72 ms 7764 KB Output is correct
15 Correct 74 ms 6916 KB Output is correct
16 Correct 62 ms 6924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2872 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 2 ms 2904 KB Output is correct
8 Correct 7 ms 3144 KB Output is correct
9 Correct 7 ms 2908 KB Output is correct
10 Correct 23 ms 6748 KB Output is correct
11 Correct 61 ms 7504 KB Output is correct
12 Correct 133 ms 10096 KB Output is correct
13 Correct 176 ms 10772 KB Output is correct
14 Correct 116 ms 9032 KB Output is correct
15 Correct 97 ms 6768 KB Output is correct
16 Correct 102 ms 6736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2872 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 2 ms 2904 KB Output is correct
8 Correct 7 ms 3144 KB Output is correct
9 Correct 7 ms 2908 KB Output is correct
10 Correct 23 ms 6748 KB Output is correct
11 Correct 61 ms 7504 KB Output is correct
12 Correct 133 ms 10096 KB Output is correct
13 Correct 176 ms 10772 KB Output is correct
14 Correct 116 ms 9032 KB Output is correct
15 Correct 97 ms 6768 KB Output is correct
16 Correct 102 ms 6736 KB Output is correct
17 Correct 73 ms 7744 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2804 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 3 ms 2908 KB Output is correct
25 Correct 11 ms 3164 KB Output is correct
26 Correct 6 ms 3164 KB Output is correct
27 Correct 18 ms 7256 KB Output is correct
28 Correct 195 ms 10508 KB Output is correct
29 Correct 197 ms 10872 KB Output is correct
30 Correct 129 ms 9144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 7384 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2872 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2972 KB Output is correct
7 Correct 2 ms 2964 KB Output is correct
8 Correct 28 ms 7304 KB Output is correct
9 Correct 17 ms 7228 KB Output is correct
10 Correct 4 ms 3356 KB Output is correct
11 Correct 53 ms 8216 KB Output is correct
12 Correct 1 ms 2872 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 4 ms 3360 KB Output is correct
18 Correct 4 ms 3356 KB Output is correct
19 Correct 19 ms 7452 KB Output is correct
20 Correct 107 ms 10532 KB Output is correct
21 Correct 46 ms 8212 KB Output is correct
22 Correct 32 ms 7680 KB Output is correct
23 Correct 149 ms 10816 KB Output is correct
24 Correct 72 ms 7764 KB Output is correct
25 Correct 74 ms 6916 KB Output is correct
26 Correct 62 ms 6924 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2872 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 2 ms 2904 KB Output is correct
34 Correct 7 ms 3144 KB Output is correct
35 Correct 7 ms 2908 KB Output is correct
36 Correct 23 ms 6748 KB Output is correct
37 Correct 61 ms 7504 KB Output is correct
38 Correct 133 ms 10096 KB Output is correct
39 Correct 176 ms 10772 KB Output is correct
40 Correct 116 ms 9032 KB Output is correct
41 Correct 97 ms 6768 KB Output is correct
42 Correct 102 ms 6736 KB Output is correct
43 Correct 73 ms 7744 KB Output is correct
44 Correct 1 ms 2648 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 1 ms 2648 KB Output is correct
47 Correct 1 ms 2652 KB Output is correct
48 Correct 1 ms 2804 KB Output is correct
49 Correct 2 ms 2908 KB Output is correct
50 Correct 3 ms 2908 KB Output is correct
51 Correct 11 ms 3164 KB Output is correct
52 Correct 6 ms 3164 KB Output is correct
53 Correct 18 ms 7256 KB Output is correct
54 Correct 195 ms 10508 KB Output is correct
55 Correct 197 ms 10872 KB Output is correct
56 Correct 129 ms 9144 KB Output is correct
57 Correct 439 ms 12216 KB Output is correct
58 Correct 21 ms 7448 KB Output is correct
59 Correct 85 ms 8244 KB Output is correct