#include<bits/stdc++.h>
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout<<"\n";
#define int long long
#define inf (int)1e18
#define pii pair<int,int>
#define vii vector<pii>
#define lb(x) x&-x
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(1<<j))
#define vi vector<int>
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
int f[50010][18][5][5];
int ans[5][5],now[5][5];
int n,k,m,q;
void combine(int ans[5][5],int a[5][5],int b[5][5])
{
fo(i,0,4) fo(j,0,4) fo(k,0,4) ans[i][j] = min(ans[i][j],a[i][k] + b[k][j]);
}
main()
{
#define name "TASK"
if(fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> k >> n >> m >> q;
fo(i,0,n / k) fo(j,0,17) fo(x,0,4) fo(y,0,4) f[i][j][x][y] = inf;
fo(i,1,m)
{
int u,v,w;
cin >> u >> v >> w;
f[u / k][0][u % k][v % k] = w;
}
fo(j,1,17) fo(i,0,(n + k - 1) / k - (1 << j))
combine(f[i][j],f[i][j - 1],f[i + (1 << (j - 1))][j - 1]);
while(q--)
{
fo(i,0,4) fo(j,0,4) ans[i][j] = inf;
fo(i,0,4) ans[i][i] = 0;
int u,v;
cin >> u >> v;
int U = u / k,V = v / k;
int del = V - U;
fod(i,17,0) if(bit(del,i))
{
fo(j,0,4) fo(k,0,4) now[j][k] = inf;
combine(now,f[U][i],ans);
memcpy(ans,now,sizeof ans);
U = U + (1 << i);
}
cout << (ans[u % k][v % k] == inf ? -1 : ans[u % k][v % k]);en;
}
}
Compilation message
toll.cpp:31:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
31 | main()
| ^~~~
toll.cpp: In function 'int main()':
toll.cpp:36:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:37:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
291 ms |
176464 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
4 ms |
4752 KB |
Output is correct |
7 |
Correct |
4 ms |
4696 KB |
Output is correct |
8 |
Correct |
268 ms |
176564 KB |
Output is correct |
9 |
Correct |
253 ms |
176576 KB |
Output is correct |
10 |
Correct |
247 ms |
176556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
128 ms |
88916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
291 ms |
176464 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
4 ms |
4752 KB |
Output is correct |
7 |
Correct |
4 ms |
4696 KB |
Output is correct |
8 |
Correct |
268 ms |
176564 KB |
Output is correct |
9 |
Correct |
253 ms |
176576 KB |
Output is correct |
10 |
Correct |
247 ms |
176556 KB |
Output is correct |
11 |
Incorrect |
128 ms |
88916 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |