#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(int i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm)
// HERE IS THE SOLUTION
int N , K , M , O;
const int LOG = 15;
signed main()
{
boost;
cin.tie(0);
cout.tie(0);
cin>>K>>N>>M>>O;
vector<vector<vii>> up(LOG + 1 , vector<vii>(K , vii(N , {N , INF})));
fore(i , M)
{
int a , b , t;
cin>>a>>b>>t;
up[0][b%K][a] = {b , t};
}
forr(lg ,1, LOG)
{
fore(i , N)
{
fore(j , K)
{
int mn = INF;
int go = 0;
fore(k , K)
{
if(up[lg - 1][j][i].second + up[lg - 1][k][up[lg - 1][j][i].first].second < mn)
{
up[lg][j][i] = {up[lg - 1][k][up[lg - 1][j][i].first].first , up[lg - 1][j][i].second + up[lg - 1][k][up[lg - 1][j][i].first].second};
}
}
}
}
}
while(O--)
{
int a , b;
cin>>a>>b;
if((b - b%K) == (a - a%K))
{
cout<<-1<<nl;
continue;
}
int ans = 0;
int k = b%K;
int cur= a;
forn(lg , LOG , 0)
{
if(up[lg][k][cur].first >= b)
continue;
int min = 1e14;
int ncur = cur;
fore(mod , K)
{
if(up[lg][mod][cur].second < min)
{
min = up[lg][mod][cur].second;
ncur = up[lg][mod][cur].first;
}
}
ans+=min;
cur = ncur;
}
int final = (ans + up[0][k][cur].second);
if(final >= (int)1e14)
{
cout<<-1<<nl;
}
else
cout<<final<<nl;
}
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:57:21: warning: unused variable 'go' [-Wunused-variable]
57 | int go = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
14424 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
21 ms |
14428 KB |
Output is correct |
9 |
Correct |
19 ms |
14428 KB |
Output is correct |
10 |
Correct |
11 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
27740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
14424 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
21 ms |
14428 KB |
Output is correct |
9 |
Correct |
19 ms |
14428 KB |
Output is correct |
10 |
Correct |
11 ms |
14428 KB |
Output is correct |
11 |
Incorrect |
37 ms |
27740 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |