#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5;
const ll INF = 1e15;
int K, N, M, Q;
vector<pii> adj[MAXN+10];
struct Node
{
vector<vector<ll>> V;
Node() { V=vector<vector<ll>>(K, vector<ll>(K, INF)); }
};
Node Z;
Node operator + (const Node &p, const Node &q)
{
Node ret=Node();
int i, j, k;
for(i=0; i<K; i++)
{
for(j=0; j<K; j++)
{
ll val=INF;
for(k=0; k<K; k++) val=min(val, p.V[i][k]+q.V[k][j]);
ret.V[i][j]=val;
}
}
return ret;
}
Node tree[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
int i, j;
tree[node]=Node();
for(i=(tl-1)*K; i<tl*K; i++) for(auto nxt : adj[i]) tree[node].V[i%K][nxt.first%K]=nxt.second;
//printf("%d %d : \n", tl, tr);
//for(i=0; i<K; i++) { for(j=0; j<K; j++) printf("%lld ", tree[node].V[i][j]); printf("\n"); }
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node]=tree[node*2]+tree[node*2+1];
//int i, j;
//printf("%d %d : \n", tl, tr);
//for(i=0; i<K; i++) { for(j=0; j<K; j++) printf("%lld ", tree[node].V[i][j]); printf("\n"); }
}
Node query(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(tr<l || r<tl) return Z;
int mid=tl+tr>>1;
return query(node*2, tl, mid, l, r)+query(node*2+1, mid+1, tr, l, r);
}
Node nodes[MAXN+10];
ll dp[MAXN+10];
int main()
{
int i, j, k;
scanf("%d%d%d%d", &K, &N, &M, &Q);
if(N%K) N=N/K*K+K;
Z=Node();
for(i=0; i<K; i++) Z.V[i][i]=0;
for(i=1; i<=M; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
adj[u].push_back({v, w});
}
for(k=1; k<N/K; k++)
{
nodes[k]=Node();
for(i=(k-1)*K; i<k*K; i++) for(auto nxt : adj[i]) nodes[k].V[i%K][nxt.first%K]=nxt.second;
}
//init(1, 1, N/K-1);
while(Q--)
{
int a, b;
scanf("%d%d", &a, &b);
for(i=0; i<N; i++) dp[i]=INF;
dp[a]=0;
for(i=a; i<=b; i++) for(auto nxt : adj[i]) dp[nxt.first]=min(dp[nxt.first], dp[i]+nxt.second);
/*
if(a/K==b/K) { printf("-1"); continue; }
//Node t=query(1, 1, N/K-1, a/K+1, b/K);
Node t=nodes[a/K+1];
for(i=a/K+2; i<=b/K; i++) t=t+nodes[i];
ll ans=t.V[a%K][b%K];
if(ans>=INF) ans=-1;
*/
if(dp[b]==INF) dp[b]=-1;
printf("%lld\n", dp[b]);
}
}
Compilation message
toll.cpp: In function 'void init(int, int, int)':
toll.cpp:43:10: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
toll.cpp:50:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
toll.cpp: In function 'Node query(int, int, int, int, int)':
toll.cpp:63:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
toll.cpp: In function 'int main()':
toll.cpp:72:9: warning: unused variable 'j' [-Wunused-variable]
int i, j, k;
^
toll.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &K, &N, &M, &Q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &u, &v, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3011 ms |
76152 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3097 ms |
76188 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
70776 KB |
Output is correct |
2 |
Correct |
76 ms |
70840 KB |
Output is correct |
3 |
Correct |
81 ms |
70916 KB |
Output is correct |
4 |
Correct |
80 ms |
70908 KB |
Output is correct |
5 |
Correct |
77 ms |
70876 KB |
Output is correct |
6 |
Correct |
76 ms |
70904 KB |
Output is correct |
7 |
Correct |
78 ms |
70944 KB |
Output is correct |
8 |
Correct |
85 ms |
71024 KB |
Output is correct |
9 |
Correct |
83 ms |
71032 KB |
Output is correct |
10 |
Correct |
229 ms |
76764 KB |
Output is correct |
11 |
Correct |
211 ms |
77560 KB |
Output is correct |
12 |
Correct |
263 ms |
78420 KB |
Output is correct |
13 |
Correct |
322 ms |
79024 KB |
Output is correct |
14 |
Correct |
263 ms |
77688 KB |
Output is correct |
15 |
Correct |
158 ms |
75752 KB |
Output is correct |
16 |
Correct |
149 ms |
75768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
70776 KB |
Output is correct |
2 |
Correct |
76 ms |
70840 KB |
Output is correct |
3 |
Correct |
81 ms |
70916 KB |
Output is correct |
4 |
Correct |
80 ms |
70908 KB |
Output is correct |
5 |
Correct |
77 ms |
70876 KB |
Output is correct |
6 |
Correct |
76 ms |
70904 KB |
Output is correct |
7 |
Correct |
78 ms |
70944 KB |
Output is correct |
8 |
Correct |
85 ms |
71024 KB |
Output is correct |
9 |
Correct |
83 ms |
71032 KB |
Output is correct |
10 |
Correct |
229 ms |
76764 KB |
Output is correct |
11 |
Correct |
211 ms |
77560 KB |
Output is correct |
12 |
Correct |
263 ms |
78420 KB |
Output is correct |
13 |
Correct |
322 ms |
79024 KB |
Output is correct |
14 |
Correct |
263 ms |
77688 KB |
Output is correct |
15 |
Correct |
158 ms |
75752 KB |
Output is correct |
16 |
Correct |
149 ms |
75768 KB |
Output is correct |
17 |
Correct |
1624 ms |
77736 KB |
Output is correct |
18 |
Correct |
77 ms |
70780 KB |
Output is correct |
19 |
Correct |
88 ms |
70776 KB |
Output is correct |
20 |
Correct |
77 ms |
70904 KB |
Output is correct |
21 |
Correct |
72 ms |
70776 KB |
Output is correct |
22 |
Correct |
78 ms |
70648 KB |
Output is correct |
23 |
Correct |
100 ms |
70968 KB |
Output is correct |
24 |
Correct |
103 ms |
71032 KB |
Output is correct |
25 |
Correct |
114 ms |
71072 KB |
Output is correct |
26 |
Correct |
108 ms |
71032 KB |
Output is correct |
27 |
Correct |
1845 ms |
76896 KB |
Output is correct |
28 |
Correct |
1999 ms |
78804 KB |
Output is correct |
29 |
Correct |
2353 ms |
79212 KB |
Output is correct |
30 |
Correct |
1813 ms |
77860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3011 ms |
76152 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |