답안 #696056

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
696056 2023-02-05T09:28:13 Z anhduc2701 Toll (BOI17_toll) C++17
0 / 100
314 ms 183588 KB
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI 3.14159265359
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define task "tnc"  
typedef long long ll;
const ll INF=1e18;
const int maxn=1e6+5;
const int mod=1e9+7;
const int mo=998244353;
using pi=pair<ll,ll>;
using vi=vector<ll>;
using pii=pair<pair<ll,ll>,ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,k,m,o;
struct Matrix{
	ll a[5][5];
	Matrix(){
		for(int i=0;i<5;i++){
			for(int j=0;j<5;j++){
				a[i][j]=INF;
			}
		}
	}
	Matrix operator+(const Matrix&b){
		Matrix res;
		for(int i=0;i<5;i++){
			for(int j=0;j<5;j++){
				res.a[i][j]=INF;
				for(int z=0;z<5;z++){
					res.a[i][j]=min(res.a[i][j],a[i][z]+b.a[z][j]);
				}
			}
		}
		return res;
	}
};
Matrix d[50005][18];
int jump[50005][18];
signed main()
{
	cin.tie(0),cout.tie(0)->sync_with_stdio(0);
    //freopen(task".inp" , "r" , stdin);
    //freopen(task".out" , "w" , stdout);
    cin>>k>>n>>m>>o;
    for(int i=0;i<n;i++){
    	jump[i][0]=i+1;
    }
    jump[n][0]=n;
    for(int i=1;i<=m;i++){
    	int a,b,t;
    	cin>>a>>b>>t;
    	d[a/k][0].a[a%k][b%k]=t;
    }
    
    int sl=(n+k-1)/k;
    
    for(int i=1;(1<<i)<=n;i++){
    	for(int j=0;j+(1<<i)-1<=n;j++){
    		jump[j][i]=jump[jump[j][i-1]][i-1];
    		d[j][i]=d[j][i-1]+d[jump[j][i-1]][i-1];
    	}
    }
    
    for(int i=1;i<=o;i++){
    	int a,b;
    	cin>>a>>b;
    	Matrix s;
    	for(int j=0;j<5;j++){
    		s.a[j][j]=0;
    		
    	}
    	if(a==b){
    		cout<<0;
    		continue;
    	}
    	int lop=a/k;
    	int lop1=b/k;
    	if(lop1<=lop){
    		cout<<-1;
    	}
    	else{
    		int hieu=lop1-lop;
    		for(int j=16;j>=0;j--){
    			if((1<<j)&hieu){
    				s=s+d[lop][j];
    				lop=jump[lop][j];
    			}
    		}
    		if(s.a[a%k][b%k]==INF)cout<<-1;
    		else cout<<s.a[a%k][b%k]<<"\n";
    	}
    }
	
    return 0;

}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:72:9: warning: unused variable 'sl' [-Wunused-variable]
   72 |     int sl=(n+k-1)/k;
      |         ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 183588 KB Output is correct
2 Incorrect 69 ms 176400 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 292 ms 183476 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 70 ms 176340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 70 ms 176340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 183588 KB Output is correct
2 Incorrect 69 ms 176400 KB Output isn't correct
3 Halted 0 ms 0 KB -