Submission #439944

# Submission time Handle Problem Language Result Execution time Memory
439944 2021-07-01T09:18:27 Z K4YAN Toll (BOI17_toll) C++17
0 / 100
3000 ms 41380 KB
//Be Name Khoda
// 16:25 | 13:30

#include<bits/stdc++.h>
#pragma GCC optimize ("unroll-loops")

using namespace std;

typedef long long ll;
typedef long double ld;
#define all(x) x.begin(), x.end()
#define pll pair<ll, ll>
#define pii pair<int, int>
#define plll pair<pll, ll>
#define piii pair<pii, int>
#define piiii pair<pii, pii>

const int mxn = 1e5 + 5;
int n, m, k, o;
int dis[mxn][18][5], arr[5][2];
vector<pii> adj[mxn], Qu;

void input() {

    for(int i = 0; i < mxn; i++) {
        for(int j = 0; j < 18; j++) {
            for(int e = 0; e < 5; e++) {
                dis[i][j][e] = 1e9;
            }
        }
    }
    cin >> k >> n >> m >> o;

    int v, u, w;
    for(int i = 0; i < m; i++) {
        cin >> v >> u >> w;
        adj[v].push_back({u, w});
        dis[v][0][u % k] = w;
    }

    for(int i = 0; i < o; i++) {
        cin >> v >> u;
        if(v / k == u / k) {
            Qu.push_back({-1, -1});
            continue;
        }
        if(v > u) {
            Qu.push_back({-1, -1});
            continue;
        }
        Qu.push_back({v, u});
    }

}

void solve() {

    for(int i = 1; i < 18; i++) {
        for(int j = 0; j < n; j++) {
            for(int w = 0; w < k; w++) {
                for(int q = 0; q < k; q++) {
                    int t = j + (k * (1 << (i - 1))) - (j % k) + q;
                    if(t > n) {
                        break;
                    }
                    dis[j][i][w] = min(dis[j][i][w], dis[j][i - 1][q] + dis[j + (k * (1 << (i - 1))) - (j % k) + q][i - 1][w]);
                }
            }
        }
    }

    int x, gn;
    vector<int> v;
    for(auto Q : Qu) {

        if(Q.first == -1) {
            cout << "-1" << endl;
            continue;
        }
        for(int i = 0; i < k; i++) {
            arr[i][0] = 1e9;
            arr[i][1] = 1e9;
        }

        for(auto g : adj[Q.first]) {
            arr[g.first % k][0] = g.second;
            v.push_back(g.first);
        }

        x = (Q.second / k) - (Q.first / k) - 1;
        for(int i = 17; i > -1; i--) {

            if(x - (1 << i) >= 0) {
                x -= (1 << i);
                // idam ine brute force bezanam az hame rasaye yek daste
                // be 2^i taye badio bbinam
                for(int j = 0; j < k; j++) {
                    for(auto g : v) {
                        arr[j][1] = min(arr[j][1], arr[g % k][0] + dis[g][i][j]);
                    }
                }
                for(int j = 0; j < k; j++) {
                    arr[j][0] = arr[j][1];
                    arr[j][1] = 1e9;
                }
            }

        }

        if(arr[Q.second % k][0] >= 1e9) {
            arr[Q.second % k][0] = -1;
        }
        cout << arr[Q.second % k][0] << endl;

    }
    

}

int main() {

    ios_base::sync_with_stdio(false);

    input(), solve();

    return 0;

}

/*
5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
*/

Compilation message

toll.cpp: In function 'void solve()':
toll.cpp:72:12: warning: unused variable 'gn' [-Wunused-variable]
   72 |     int x, gn;
      |            ^~
# Verdict Execution time Memory Grader output
1 Execution timed out 3078 ms 40864 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3075 ms 41380 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 37836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 37836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3078 ms 40864 KB Time limit exceeded
2 Halted 0 ms 0 KB -