답안 #1016423

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016423 2024-07-08T05:03:11 Z Whisper Toll (BOI17_toll) C++17
0 / 100
162 ms 86864 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX_NODE            50005
#define MAX_EDGE            500005


int numNode, numEdge, numQuery, k;
struct Matrix{
    int N, M;
    vector<vector<int>> F;
    Matrix(): N(0), M(0){}
    void init(int _N, int _M){
        this -> N = _N, this -> M = _M;
        F.resize(N, vector<int>(M, INF));
    }

    friend Matrix operator * (const Matrix &a, const Matrix &b){
        Matrix res; res.init(a.N, b.M);
        REP(i, a.N) REP(j, b.M) REP(k, a.M){
            minimize(res.F[i][j], a.F[i][k] + b.F[k][j]);
            if (res.F[i][j] > INF) res.F[i][j] = INF;
        }
        return res;
    }
};

Matrix power(Matrix a, int exp){
    Matrix res; res.init(a.N, a.N);
    REP(i, a.N) res.F[i][i] = 1;

    for (; exp > 0; exp >>= 1, a = (a * a)) if(exp & 1){
        res = (res * a);
    }
    return res;
}

Matrix dp[MAX_NODE][17];

void process(void){
    cin >> k >> numNode >> numEdge >> numQuery;
    int n = (numNode - 1) / k;
    for (int i = 0; i <= n; ++i){
        for (int j = 0; j <= 16; ++j) dp[i][j].init(k, k);
    }
    for (int i = 1; i <= numEdge; ++i){
        int u, v, w; cin >> u >> v >> w;
        minimize(dp[u / k][0].F[u % k][v % k], w);
    }

    for (int k = 1; k < 16; ++k){
        for (int i = 0; i + MASK(k) - 1 <= n; ++i){
            dp[i][k] = dp[i][k - 1] * dp[i + MASK(k - 1)][k - 1];
        }
    }


    for (int i = 1; i <= numQuery; ++i){
        int u, v; cin >> u >> v;
        int l = u / k, r = v / k;
        int dif = r - l;
        Matrix res; res.init(k, k);
        REP(x, k) REP(y, k) res.F[x][y] = 0;
        for (int j = 15; j >= 0; --j) if(BIT(dif, j)){
            res = res * dp[l][j];
            l |= MASK(j);
        }
        cout << (res.F[u % k][v % k] >= INF ? -1 : res.F[u % k][v % k]) << '\n';
    }
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);

    process();
}



# 결과 실행 시간 메모리 Grader output
1 Incorrect 152 ms 86832 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 162 ms 86864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33624 KB Output is correct
2 Incorrect 8 ms 33628 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33624 KB Output is correct
2 Incorrect 8 ms 33628 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 152 ms 86832 KB Output isn't correct
2 Halted 0 ms 0 KB -