제출 #1063366

#제출 시각아이디문제언어결과실행 시간메모리
1063366I_FloPPed21Toll (BOI17_toll)C++14
100 / 100
85 ms86304 KiB
    #include <bits/stdc++.h>
    using namespace std;
    int dp[50005][17][5][5],n,k,m,q,temp[5][5],ans[5][5];
    void combine(int target[5][5],int a[5][5],int b[5][5])
    {
        for(int x=0; x<k; x++)
            for(int y=0; y<k; y++)
            {
                for(int z=0; z<k; z++)
                {
                    target[x][y]=min(target[x][y],a[x][z]+b[z][y]);
                }
            }
    }
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cin>>k>>n>>m>>q;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<17; j++)
                for(int x=0; x<k; x++)
                    for(int y=0; y<k; y++)
                        dp[i][j][x][y] =5e8+1;
        }
        for(int i=1; i<=m; i++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            dp[a/k][0][a%k][b%k]=c;
        }

        for(int i=1; i<17; i++)
        {
            for(int j=0; j+(1<<i)<(n+k-1)/k; j++)
            {
                combine(dp[j][i],dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
            }
        }

        while(q--)
        {
            int a,b;
            cin>>a>>b;
            int curent=a/k;
            int dest=b/k;
            for(int i=0; i<k; i++)
                for(int j=0; j<k; j++)
                {
                    ans[i][j] =5e8+1;
                    temp[i][j] =5e8+1;
                }
            for(int i = 0 ; i <5; i++)
                ans[i][i] =0;
            for(int i =16; i>=0; i--)
            {
                if(curent+(1<<i) >dest)
                    continue;
                combine(temp,ans,dp[curent][i]);
                for(int t=0; t<k; t++)
                    for(int j=0; j<k; j++)
                    {

                        ans[t][j]=temp[t][j];
                        temp[t][j] =5e8+1;
                    }
                curent+=(1<<i);
            }

            if(ans[a%k][b%k]==5e8+1)
                cout<<-1<<'\n';
            else
                cout<<ans[a%k][b%k]<<'\n';
        }

        return 0;
    }
#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...