Submission #441346

#TimeUsernameProblemLanguageResultExecution timeMemory
441346cpp219Toll (BOI17_toll)C++14
100 / 100
330 ms88020 KiB
#pragma GCC optimization O2
#pragma GCC optimization "unroll-loop"
#pragma target ("avx2")

#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 5e4 + 9;
const ll Log2 = 20;
const ll inf = 1e9 + 7;
vector<ll> g[N];
ll k,n,m,Q,x,y,w,dp[N][17][5][5],tmp[5][5],ans[5][5];

void Reset(ll a[5][5]){
    for (ll i = 0;i < k;i++)
        for (ll j = 0;j < k;j++) a[i][j] = inf;
}

void Copy(){
     for (ll i = 0;i < k;i++)
        for (ll j = 0;j < k;j++) ans[i][j] = tmp[i][j];
}

void combine(ll tar[5][5],ll a[5][5],ll b[5][5]){
    for (ll i = 0;i < k;i++)
        for (ll j = 0;j < k;j++)
            for (ll l = 0;l < k;l++){
                tar[i][j] = min(tar[i][j],a[i][l] + b[l][j]);
            }
}

void out(ll a[5][5]){
    for (ll i = 0;i < 5;i++){
        for (ll j = 0;j < 5;j++) cout<<a[i][j]<<" ";
        cout<<"\n";
    }
    exit(0);
}

int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "tst"
    if (fopen(task".INP","r")){
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    cin>>k>>n>>m>>Q;
    for (ll i = 0;i < n;i++)
        for (ll j = 0;j < 17;j++) Reset(dp[i][j]);
    while(m--){
        cin>>x>>y>>w;
        dp[x/k][0][x%k][y%k] = w;
    }
    for (ll j = 1;j < 17;j++){
        for (ll i = 0;i <= n;i++){
            if (i + (1 << (j - 1)) > n) continue;
            combine(dp[i][j],dp[i][j - 1],dp[i + (1 << (j - 1))][j - 1]);

        }
    }
    //out(dp[0][0]);
    while(Q--){
        cin>>x>>y;
        ll cur = x/k,tar = y/k; Reset(ans);
        for (ll i = 0;i < k;i++) ans[i][i] = 0;
        for (ll i = 16;i >= 0;i--){
            if (cur + (1 << i) > tar) continue;
            //out(dp[cur][i]);
            Reset(tmp); combine(tmp,ans,dp[cur][i]); Copy();

            cur += (1 << i);
        }
        if (ans[x % k][y % k] != inf && x/k < y/k) cout<<ans[x % k][y % k];
        else cout<<-1; cout<<"\n";
    }
}

Compilation message (stderr)

toll.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization O2
      | 
toll.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
toll.cpp:3: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
    3 | #pragma target ("avx2")
      | 
toll.cpp: In function 'int main()':
toll.cpp:78:9: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
   78 |         else cout<<-1; cout<<"\n";
      |         ^~~~
toll.cpp:78:24: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
   78 |         else cout<<-1; cout<<"\n";
      |                        ^~~~
toll.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...