Submission #696750

# Submission time Handle Problem Language Result Execution time Memory
696750 2023-02-07T07:37:58 Z vjudge1 Toll (BOI17_toll) C++17
0 / 100
409 ms 250532 KB
#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define gcd __gcd
#define endl '\n'
const int N=200050,M=1000000007;
const ll INF=1e18;

struct matrix{
	ll n, m;
	vector<vector<ll>> a;
	
	matrix(ll x, ll y, ll v=0){
		n=x, m=y;
		a.assign(n, vector<ll>(m, v));
	}
	
	auto & operator [](ll i){return a[i];}
	const auto & operator [](ll i)const{return a[i];}
	
	matrix()=default;
	matrix(const vector<vector<ll>> & x):a(x){
		n=a.size();
		m=a[0].size();
	}
	
	friend ostream& operator <<(ostream& stream, const matrix &x){
		for(auto i:x.a){
			for(auto j:i) stream<<j<<" ";
			stream<<endl;
		}
		return stream;
	}
	
	static matrix identity(ll n){
		matrix res(n, n);
		for(ll i=0; i<n; i++) res[i][i]=1;
		return res;
	}
	
	matrix operator *(const matrix b){
		matrix a=*this, res(a.n, b.m, INF);
		// cout<<b.n<<" "<<b.m<<endl;
		for(ll i=0; i<a.n; i++)
			for(ll j=0; j<b.m; j++)
				for(ll k=0; k<a.m; k++) 
					if(a[i][k]!=INF && b[k][j]!=INF)
						res[i][j]=min(res[i][j], a[i][k]+b[k][j]);
		return res;
	}
	
	matrix pow(ll k){
		matrix res=identity(this->n), a=*this;
		while(k){
			if(k&1) res=res*a;
			a=a*a; k>>=1;
		}
		return res;
	}
};

ll k, n, m, q, a, b, t;

signed main(){
	ios_base::sync_with_stdio(NULL); 
	cin.tie(nullptr); cout.tie(nullptr);
	cin>>k>>n>>m>>q;
	matrix p[n][15];
	for(ll i=0; i<n; i++)
		for(ll j=0; j<15; j++)  p[i][j]=matrix(k, k, INF);
		
	while(m--){
		cin>>a>>b>>t;
		if(a>b) swap(a, b);
		if(a/k+1==b/k){
			// cout<<a<<" "<<b<<endl;
			p[a/k][0][a%k][b%k]=t;
		}
	}
	for(ll i=1; (1<<(i-1))<n; i++)
		for(ll j=0; j*k<n; j++){
			// cout<<j<<" "<<i<<" "<<j+(1<<i)<<endl;
			// cout<<p[j][i-1]<<endl<<endl<<p[j+(1<<(i-1))][i-1]<<"\n\n";
			p[j][i]=p[j][i-1]*p[j+(1<<(i-1))][i-1];
			// cout<<p[j][i]<<"\n\n\n";
		}
	while(q--){
		cin>>a>>b;
		ll now=a/k, l=b/k-a/k;
		bool bl=0;
		matrix res(k, k, 0);
		for(ll i=0; i<15; i++)
			if(l&(1<<i)){
				if(!bl) res=p[now][i];
				else res=p[now][i]*res;
				// cout<<p[now][i]<<endl<<res<<endl;
				now+=i<<1;
				bl=1;
			}
		cout<<(res[a%k][b%k]==INF? -1: res[a%k][b%k])<<endl;
	}
    return 0;
}	
# Verdict Execution time Memory Grader output
1 Runtime error 183 ms 155296 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 409 ms 250532 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 183 ms 155296 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -