답안 #878104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878104 2023-11-24T05:26:53 Z serkanrashid Toll (BOI17_toll) C++14
100 / 100
311 ms 54492 KB
#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

struct edge
{
    int ver,dist;
    edge(){};
    edge(int vi, int di)
    {
        ver = vi;
        dist = di;
    }
    bool operator<(const edge&ed) const
    {
        return dist>ed.dist;
    }
};

const int maxn = 50005;

int k,n,m,o;
vector<edge>g[maxn],v[maxn];
unsigned int pref[maxn][5][22],suff[maxn][5][22];
unsigned int inf = -1;

void dijkstra_pref(int beg, int i, int lvl, int l, int r)
{
    pref[beg][i][lvl] = 0;
    priority_queue<edge>q;
    q.push({beg,0});
    while(!q.empty())
    {
        edge e = q.top();
        q.pop();
        if(e.dist<=pref[e.ver][i][lvl])
        {
            for(edge nb:v[e.ver])
            {
                if(l<=nb.ver/k&&nb.ver/k<=r&&nb.dist+e.dist<pref[nb.ver][i][lvl])
                {
                    nb.dist += e.dist;
                    pref[nb.ver][i][lvl] = nb.dist;
                    q.push(nb);
                }
            }
        }
    }
}

void dijkstra_suff(int beg, int i, int lvl, int l, int r)
{
    suff[beg][i][lvl] = 0;
    priority_queue<edge>q;
    q.push({beg,0});
    while(!q.empty())
    {
        edge e = q.top();
        q.pop();
        if(e.dist<=suff[e.ver][i][lvl])
        {
            for(edge nb:g[e.ver])
            {
                if(l<=nb.ver/k&&nb.ver/k<=r&&nb.dist+e.dist<suff[nb.ver][i][lvl])
                {
                    nb.dist += e.dist;
                    suff[nb.ver][i][lvl] = nb.dist;
                    q.push(nb);
                }
            }
        }
    }
}

void divide(int l, int r, int lvl)
{
    if(l>=r) return;
    int mid = (l+r)/2;
    for(int i=0;i<k;i++)
    {
        int beg = mid*k + i;
        dijkstra_pref(beg,i,lvl,l,r);
        dijkstra_suff(beg,i,lvl,l,r);
    }
    divide(l,mid-1,lvl+1);
    divide(mid+1,r,lvl+1);
}

int query(int l, int r, int lvl, int a, int b)
{
    int mid = (l+r)/2;
    if(a/k<=mid&&mid<=b/k)
    {
        long long ans = inf;
        for(int i=0;i<k;i++)
        {
            if(pref[a][i][lvl]!=inf&&suff[b][i][lvl]!=inf)
                ans = min(ans,(long long)pref[a][i][lvl]+suff[b][i][lvl]);
        }
        if(ans==inf) return -1;
        return ans;
    }
    if(b/k<mid) return query(l,mid-1,lvl+1,a,b);
    return query(mid+1,r,lvl+1,a,b);
}

void read()
{
    cin >> k >> n >> m >> o;
    int a,b,t;
    for(int i=1;i<=m;i++)
    {
        cin >> a >> b >> t;
        g[a].push_back({b,t});
        v[b].push_back({a,t});
    }
    memset(pref,-1,sizeof(pref));
    memset(suff,-1,sizeof(suff));
    divide(0,n/k,0);
    for(int i=1;i<=o;i++)
    {
        cin >> a >> b;
        cout << query(0,n/k,0,a,b) << endl;
    }
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	read();
return 0;
}

Compilation message

toll.cpp: In function 'void dijkstra_pref(int, int, int, int, int)':
toll.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
   37 |         if(e.dist<=pref[e.ver][i][lvl])
      |            ~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:41:60: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
   41 |                 if(l<=nb.ver/k&&nb.ver/k<=r&&nb.dist+e.dist<pref[nb.ver][i][lvl])
      |                                              ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp: In function 'void dijkstra_suff(int, int, int, int, int)':
toll.cpp:61:18: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
   61 |         if(e.dist<=suff[e.ver][i][lvl])
      |            ~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:65:60: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
   65 |                 if(l<=nb.ver/k&&nb.ver/k<=r&&nb.dist+e.dist<suff[nb.ver][i][lvl])
      |                                              ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 48976 KB Output is correct
2 Correct 7 ms 45660 KB Output is correct
3 Correct 6 ms 45660 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 8 ms 45916 KB Output is correct
6 Correct 8 ms 45916 KB Output is correct
7 Correct 7 ms 45912 KB Output is correct
8 Correct 42 ms 48944 KB Output is correct
9 Correct 33 ms 48820 KB Output is correct
10 Correct 16 ms 45656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 48976 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45656 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 7 ms 45660 KB Output is correct
6 Correct 7 ms 45660 KB Output is correct
7 Correct 8 ms 45916 KB Output is correct
8 Correct 9 ms 45916 KB Output is correct
9 Correct 44 ms 49756 KB Output is correct
10 Correct 204 ms 53000 KB Output is correct
11 Correct 113 ms 50512 KB Output is correct
12 Correct 62 ms 50056 KB Output is correct
13 Correct 230 ms 53620 KB Output is correct
14 Correct 127 ms 50512 KB Output is correct
15 Correct 166 ms 49896 KB Output is correct
16 Correct 159 ms 49660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45912 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 7 ms 45868 KB Output is correct
6 Correct 7 ms 45916 KB Output is correct
7 Correct 7 ms 45948 KB Output is correct
8 Correct 11 ms 45912 KB Output is correct
9 Correct 9 ms 45916 KB Output is correct
10 Correct 53 ms 49748 KB Output is correct
11 Correct 129 ms 50492 KB Output is correct
12 Correct 206 ms 53168 KB Output is correct
13 Correct 211 ms 53408 KB Output is correct
14 Correct 172 ms 51844 KB Output is correct
15 Correct 149 ms 49492 KB Output is correct
16 Correct 151 ms 49608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45912 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 7 ms 45868 KB Output is correct
6 Correct 7 ms 45916 KB Output is correct
7 Correct 7 ms 45948 KB Output is correct
8 Correct 11 ms 45912 KB Output is correct
9 Correct 9 ms 45916 KB Output is correct
10 Correct 53 ms 49748 KB Output is correct
11 Correct 129 ms 50492 KB Output is correct
12 Correct 206 ms 53168 KB Output is correct
13 Correct 211 ms 53408 KB Output is correct
14 Correct 172 ms 51844 KB Output is correct
15 Correct 149 ms 49492 KB Output is correct
16 Correct 151 ms 49608 KB Output is correct
17 Correct 107 ms 50512 KB Output is correct
18 Correct 7 ms 45660 KB Output is correct
19 Correct 6 ms 45660 KB Output is correct
20 Correct 6 ms 45660 KB Output is correct
21 Correct 6 ms 45660 KB Output is correct
22 Correct 6 ms 45660 KB Output is correct
23 Correct 8 ms 45912 KB Output is correct
24 Correct 8 ms 45916 KB Output is correct
25 Correct 11 ms 45916 KB Output is correct
26 Correct 10 ms 45916 KB Output is correct
27 Correct 45 ms 49820 KB Output is correct
28 Correct 196 ms 52820 KB Output is correct
29 Correct 222 ms 53676 KB Output is correct
30 Correct 181 ms 51872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 48976 KB Output is correct
2 Correct 7 ms 45660 KB Output is correct
3 Correct 6 ms 45660 KB Output is correct
4 Correct 6 ms 45660 KB Output is correct
5 Correct 8 ms 45916 KB Output is correct
6 Correct 8 ms 45916 KB Output is correct
7 Correct 7 ms 45912 KB Output is correct
8 Correct 42 ms 48944 KB Output is correct
9 Correct 33 ms 48820 KB Output is correct
10 Correct 16 ms 45656 KB Output is correct
11 Correct 108 ms 48976 KB Output is correct
12 Correct 6 ms 45660 KB Output is correct
13 Correct 6 ms 45656 KB Output is correct
14 Correct 6 ms 45660 KB Output is correct
15 Correct 7 ms 45660 KB Output is correct
16 Correct 7 ms 45660 KB Output is correct
17 Correct 8 ms 45916 KB Output is correct
18 Correct 9 ms 45916 KB Output is correct
19 Correct 44 ms 49756 KB Output is correct
20 Correct 204 ms 53000 KB Output is correct
21 Correct 113 ms 50512 KB Output is correct
22 Correct 62 ms 50056 KB Output is correct
23 Correct 230 ms 53620 KB Output is correct
24 Correct 127 ms 50512 KB Output is correct
25 Correct 166 ms 49896 KB Output is correct
26 Correct 159 ms 49660 KB Output is correct
27 Correct 7 ms 45660 KB Output is correct
28 Correct 6 ms 45660 KB Output is correct
29 Correct 6 ms 45912 KB Output is correct
30 Correct 6 ms 45660 KB Output is correct
31 Correct 7 ms 45868 KB Output is correct
32 Correct 7 ms 45916 KB Output is correct
33 Correct 7 ms 45948 KB Output is correct
34 Correct 11 ms 45912 KB Output is correct
35 Correct 9 ms 45916 KB Output is correct
36 Correct 53 ms 49748 KB Output is correct
37 Correct 129 ms 50492 KB Output is correct
38 Correct 206 ms 53168 KB Output is correct
39 Correct 211 ms 53408 KB Output is correct
40 Correct 172 ms 51844 KB Output is correct
41 Correct 149 ms 49492 KB Output is correct
42 Correct 151 ms 49608 KB Output is correct
43 Correct 107 ms 50512 KB Output is correct
44 Correct 7 ms 45660 KB Output is correct
45 Correct 6 ms 45660 KB Output is correct
46 Correct 6 ms 45660 KB Output is correct
47 Correct 6 ms 45660 KB Output is correct
48 Correct 6 ms 45660 KB Output is correct
49 Correct 8 ms 45912 KB Output is correct
50 Correct 8 ms 45916 KB Output is correct
51 Correct 11 ms 45916 KB Output is correct
52 Correct 10 ms 45916 KB Output is correct
53 Correct 45 ms 49820 KB Output is correct
54 Correct 196 ms 52820 KB Output is correct
55 Correct 222 ms 53676 KB Output is correct
56 Correct 181 ms 51872 KB Output is correct
57 Correct 311 ms 54492 KB Output is correct
58 Correct 39 ms 49748 KB Output is correct
59 Correct 116 ms 50684 KB Output is correct