#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e4+5;
const int inf=1e9;
vector<int> adj[mxn];
vector<int> tps[mxn];
int l[mxn],r[mxn],w[mxn];
int k,m,n;
int d[mxn][6][6];
int res[mxn];
struct query {
int a,b;
int ind;
query(int x,int y,int z) : a(x),b(y),ind(z) {};
};
void solve(int b, int e, vector<query> q) {
if(q.size() == 0) return;
if(b == e) {
for(auto i : q) res[i.ind] = -1;
return;
}
int mid = b + e >> 1;
for(int i = 0; i < k; i++) {
for(int j = 0; j < k; j++) {
d[mid][i][j] = i == j ? 0 : inf;
}
}
for(int i = mid - 1; i >= b; i--) {
for(int j = 0; j < k; j++) {
for(int x = 0; x < k; x++) d[i][j][x] = inf;
int u = i * k + j;
for(int z : adj[u]) {
int v = r[z];
for(int x = 0; x < k; x++) {
d[i][j][x] = min(d[i][j][x], d[i+1][v%k][x] + w[z]);
}
}
}
}
for(int i = mid + 1; i <= e; i++) {
for(int j = 0; j < k; j++) {
for(int x = 0; x < k; x++) d[i][j][x] = inf;
int u = i * k + j;
for(int z : tps[u]) {
int v = l[z];
for(int x = 0; x < k; x++) {
d[i][j][x] = min(d[i][j][x], d[i-1][v%k][x] + w[z]);
}
}
}
}
vector<query> tm[2];
for(auto i : q) {
int x = i.a / k;
int y = i.b / k;
if(x<=mid && y >= mid) {
int ans = inf;
for(int j = 0; j < k; j++) {
ans = min(ans,d[x][i.a % k][j]+d[y][i.b % k][j]);
}
res[i.ind] = ans >= inf ? -1 : ans;
continue;
}
tm[x > mid].push_back(i);
}
solve(b,mid,tm[0]);
solve(mid+1,e,tm[1]);
}
int main() {
int o;
cin>>k>>n>>m>>o;
for(int i = 1; i <= m; i++) {
cin>>l[i]>>r[i]>>w[i];
adj[l[i]].push_back(i);
tps[r[i]].push_back(i);
}
vector<query> qs;
for(int i = 1; i <= o; i++) {
int a,b;
cin>>a>>b;
qs.push_back(query(a,b,i));
}
solve(0,(n + k - 1) / k,qs);
for(int i = 1; i <= o; i++) {
cout<<res[i]<<endl;
}
}
Compilation message
toll.cpp: In function 'void solve(int, int, std::vector<query>)':
toll.cpp:25:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = b + e >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
800 KB |
Output is correct |
4 |
Correct |
1 ms |
768 KB |
Output is correct |
5 |
Correct |
1 ms |
768 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
4 ms |
1024 KB |
Output is correct |
8 |
Correct |
8 ms |
1024 KB |
Output is correct |
9 |
Correct |
7 ms |
1024 KB |
Output is correct |
10 |
Runtime error |
2 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
800 KB |
Output is correct |
4 |
Correct |
1 ms |
768 KB |
Output is correct |
5 |
Correct |
1 ms |
768 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
4 ms |
1024 KB |
Output is correct |
8 |
Correct |
8 ms |
1024 KB |
Output is correct |
9 |
Correct |
7 ms |
1024 KB |
Output is correct |
10 |
Runtime error |
2 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |