이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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=0x3f3f3f3f3f3f3f3f;
ll k, n, m, q, a, b, t;
map<pii, ll> w;
ll len(ll a, ll b){
ll res[k], tmp[k], x=a/k, xe=b/k, p=x*k;
for(ll i=0; i<k; i++){
res[i]=tmp[i]=INF;
if(w[{a, p+k+i}]){
res[i]=w[{a, p+k+i}];
}
}
p+=k;
while(p/k<xe){
for(ll i=0; i<k; i++){
for(ll j=0; j<k; j++){
if(res[i] && w[{p+i, p+k+j}])
tmp[j]=min(tmp[j], res[i]+w[{p+i, p+k+j}]);
}
}
for(ll i=0; i<k; i++) res[i]=tmp[i];
p+=k;
}
return res[b%k]==INF? -1: res[b%k];
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(nullptr); cout.tie(nullptr);
cin>>k>>n>>m>>q;
while(m--){
cin>>a>>b>>t;
if(a>b) swap(a, b);
// cout<<a<<" "<<b<<" ";
w[{a, b}]=t;
// cout<<w[{a, b}]<<endl;
}
while(q--){
cin>>a>>b;
cout<<len(a, b)<<endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |