Submission #378121

#TimeUsernameProblemLanguageResultExecution timeMemory
378121couplefireToll (BOI17_toll)C++17
100 / 100
1708 ms11756 KiB
#include <bits/stdc++.h>
using namespace std;
#define MAXN 50005
#define BLOCK 500
#define INF 10000000000000000ll

int len, n, m, q;
vector<pair<int, int>> adj[MAXN];
vector<pair<int, int>> revadj[MAXN];
long long dist[MAXN/BLOCK+10][MAXN/BLOCK+10][5][5];

long long bfs(int a, int b){
    if(a >= n || b >= n) return INF;
    if(b/len <= a/len) return INF;
    long long dp[5*BLOCK+100];
    fill(dp, dp+5*BLOCK+100, INF);
    dp[0] = 0;
    queue<int> q;
    q.push(a);
    while(!q.empty() && q.front() < b){
        int v = q.front();
        // cout << v << endl;
        q.pop();
        if(dp[v-a] == INF) continue;
        for(auto u : adj[v]){
            if(dp[u.first-a] == INF){
                // cout << u.first << endl;
                q.push(u.first);
            }
            // cout << u.first << endl;
            dp[u.first-a] = min(dp[u.first-a], dp[v-a]+u.second);
        }
    }
    return dp[b-a];
}

int main(){
    // freopen("a.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> len >> n >> m >> q;
    for(int i = 0; i<m; i++){
        int a, b, w; cin >> a >> b >> w;
        adj[a].push_back({b, w});
        revadj[b].push_back({a, w});
    }
    for(int i = ((n-1)/len/BLOCK)*BLOCK; i>=0; i -= BLOCK){
        for(int a = 0; a<len; a++){
            for(int b = 0; b<len; b++){
                dist[i/BLOCK][i/BLOCK][a][b] = INF;
                if(a == b) dist[i/BLOCK][i/BLOCK][a][b] = 0;
            }
            for(int j = i+BLOCK; j<=(n-1)/len; j+=BLOCK){
                for(int b = 0; b<len; b++){
                    if(j == i+BLOCK){
                        dist[i/BLOCK][j/BLOCK][a][b] = bfs(i*len+a, j*len+b);
                        continue;
                    }
                    dist[i/BLOCK][j/BLOCK][a][b] = INF;
                    for(int c = 0; c<len; c++){
                        dist[i/BLOCK][j/BLOCK][a][b] = min(dist[i/BLOCK][j/BLOCK][a][b], dist[i/BLOCK][j/BLOCK-1][a][c]+dist[j/BLOCK-1][j/BLOCK][c][b]);
                    }
                }
            }
        }
    }
    // cout << ceil((BLOCK+1.0)/BLOCK) << endl;
    while(q--){
        int a, b; cin >> a >> b;
        int ca = a%len, cb = b%len;
        int la = a/len, lb = b/len;
        if(lb <= la){
            cout << -1 << endl;
            continue;
        }
        if(la/BLOCK == lb/BLOCK){
            long long ans = bfs(a, b);
            if(ans == INF) cout << -1 << endl;
            else cout << ans << endl;
            continue;
        }
        // cout << "hi" << endl;
        int na = ceil((la+0.0)/BLOCK)*BLOCK;
        int pb = lb/BLOCK*BLOCK;
        long long dp[5*BLOCK+100];
        fill(dp, dp+5*BLOCK+100, INF);
        dp[0] = 0;
        queue<int> q;
        q.push(a);
        while(!q.empty() && q.front() < na*len+len){
            int v = q.front();
            q.pop();
            if(dp[v-a] == INF) continue;
            for(auto u : adj[v]){
                if(dp[u.first-a] == INF) q.push(u.first);
                dp[u.first-a] = min(dp[u.first-a], dp[v-a]+u.second);
            }
        }
        long long revdp[5*BLOCK+100];
        fill(revdp, revdp+5*BLOCK+100, INF);
        revdp[0] = 0;
        queue<int> revq;
        revq.push(b);
        while(!revq.empty() && revq.front() > pb*len-1){
            int v = revq.front();
            revq.pop();
            if(revdp[b-v] == INF) continue;
            for(auto u : revadj[v]){
                if(revdp[b-u.first] == INF) revq.push(u.first);
                revdp[b-u.first] = min(revdp[b-u.first], revdp[b-v]+u.second);
            }
        }
        long long ans = INF;
        for(int i = 0; i<len; i++){
            for(int j = 0; j<len; j++){
                long long v1, v2;
                if(na*len+i-a < 0) v1 = INF;
                else v1 = dp[na*len+i-a];
                if(b-(pb*len+j) < 0) v2 = INF;
                else v2 = revdp[b-(pb*len+j)];
                ans = min(ans, v1+dist[na/BLOCK][pb/BLOCK][i][j]+v2);
            }
        }
        if(ans == INF) cout << -1 << endl;
        else cout << ans << endl;
    }
}

Compilation message (stderr)

toll.cpp: In function 'int main()':
toll.cpp:70:13: warning: unused variable 'ca' [-Wunused-variable]
   70 |         int ca = a%len, cb = b%len;
      |             ^~
toll.cpp:70:25: warning: unused variable 'cb' [-Wunused-variable]
   70 |         int ca = a%len, cb = b%len;
      |                         ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...