#include <bits/stdc++.h>
using namespace std;
int k,n,m,o;
vector<pair<int,int> > v[200001],op[200001];
void read()
{
cin>>k>>n>>m>>o;
for(int i=1;i<=m;i++)
{
int a,b,t;
cin>>a>>b>>t;
v[a].push_back({b,t});
op[b].push_back({a,t});
}
}
pair<int,int> q[200001];
long long p[200001];
void subt1()
{
for(int i=0;i<n;i++)
{
p[i+1]=1e9;
for(int j=0;j<v[i].size();j++)
{
long long nb=v[i][j].second;
p[i+1]=min(p[i+1],nb);
}
p[i+1]+=p[i];
}
for(int i=1;i<=o;i++)
if(p[q[i].second]-p[q[i].first]<1e9)cout<<p[q[i].second]-p[q[i].first]<<endl;
else cout<<-1<<endl;
exit(0);
}
long long dp[200001];
void subt2()
{
int lm=n;
for(int i=1;i<n;i++)
{
dp[i]=1e9;
for(int j=0;j<op[i].size();j++)
{
pair<int,long long> nb=op[i][j];
dp[i]=min(dp[i],dp[nb.first]+nb.second);
}
}
for(int i=1;i<=o;i++)
if(dp[q[i].second]<1e9)cout<<dp[q[i].second]<<endl;
else cout<<-1<<endl;
exit(0);
}
int lf=0;
long long dp2[50001];
void print(int a,int b)
{
for(int i=0;i<a;i++)
dp2[i]=1e9;
dp2[a]=0;
for(int i=a+1;i<=b;i++)
{
dp2[i]=1e9;
for(int j=0;j<op[i].size();j++)
{
pair<int,long long> nb=op[i][j];
dp2[i]=min(dp2[i],dp2[nb.first]+nb.second);
}
}
if(dp2[b]<1e9)cout<<dp2[b]<<endl;
else cout<<-1<<endl;
}
void solve()
{
for(int i=1;i<=o;i++)
{
cin>>q[i].first>>q[i].second;
lf=max(lf,q[i].first);
}
if(k==1)subt1();
if(lf==0)subt2();
for(int i=1;i<=o;i++)
{
print(q[i].first,q[i].second);
}
}
int main() {
read();
solve();
return 0;
}
Compilation message
toll.cpp: In function 'void subt1()':
toll.cpp:27:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int j=0;j<v[i].size();j++)
| ~^~~~~~~~~~~~
toll.cpp: In function 'void subt2()':
toll.cpp:49:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int j=0;j<op[i].size();j++)
| ~^~~~~~~~~~~~~
toll.cpp:45:9: warning: unused variable 'lm' [-Wunused-variable]
45 | int lm=n;
| ^~
toll.cpp: In function 'void print(int, int)':
toll.cpp:74:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int j=0;j<op[i].size();j++)
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
15976 KB |
Output is correct |
2 |
Correct |
4 ms |
12628 KB |
Output is correct |
3 |
Correct |
4 ms |
12624 KB |
Output is correct |
4 |
Correct |
4 ms |
12624 KB |
Output is correct |
5 |
Correct |
6 ms |
12624 KB |
Output is correct |
6 |
Correct |
6 ms |
12792 KB |
Output is correct |
7 |
Correct |
6 ms |
12624 KB |
Output is correct |
8 |
Correct |
50 ms |
16120 KB |
Output is correct |
9 |
Correct |
58 ms |
15848 KB |
Output is correct |
10 |
Correct |
21 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
16200 KB |
Output is correct |
2 |
Correct |
4 ms |
12624 KB |
Output is correct |
3 |
Correct |
5 ms |
12624 KB |
Output is correct |
4 |
Correct |
3 ms |
12676 KB |
Output is correct |
5 |
Correct |
4 ms |
12624 KB |
Output is correct |
6 |
Correct |
3 ms |
12624 KB |
Output is correct |
7 |
Correct |
22 ms |
12796 KB |
Output is correct |
8 |
Correct |
20 ms |
12768 KB |
Output is correct |
9 |
Correct |
55 ms |
15952 KB |
Output is correct |
10 |
Correct |
108 ms |
18088 KB |
Output is correct |
11 |
Correct |
81 ms |
16168 KB |
Output is correct |
12 |
Correct |
78 ms |
15884 KB |
Output is correct |
13 |
Correct |
132 ms |
18248 KB |
Output is correct |
14 |
Correct |
64 ms |
16020 KB |
Output is correct |
15 |
Correct |
55 ms |
15444 KB |
Output is correct |
16 |
Correct |
55 ms |
15444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12624 KB |
Output is correct |
2 |
Correct |
4 ms |
12624 KB |
Output is correct |
3 |
Correct |
5 ms |
12628 KB |
Output is correct |
4 |
Correct |
5 ms |
12476 KB |
Output is correct |
5 |
Correct |
4 ms |
12628 KB |
Output is correct |
6 |
Correct |
5 ms |
12628 KB |
Output is correct |
7 |
Correct |
4 ms |
12628 KB |
Output is correct |
8 |
Correct |
6 ms |
12884 KB |
Output is correct |
9 |
Correct |
6 ms |
12884 KB |
Output is correct |
10 |
Correct |
32 ms |
16724 KB |
Output is correct |
11 |
Correct |
68 ms |
17620 KB |
Output is correct |
12 |
Correct |
103 ms |
20044 KB |
Output is correct |
13 |
Correct |
115 ms |
20820 KB |
Output is correct |
14 |
Correct |
81 ms |
19020 KB |
Output is correct |
15 |
Correct |
54 ms |
16716 KB |
Output is correct |
16 |
Correct |
69 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12624 KB |
Output is correct |
2 |
Correct |
4 ms |
12624 KB |
Output is correct |
3 |
Correct |
5 ms |
12628 KB |
Output is correct |
4 |
Correct |
5 ms |
12476 KB |
Output is correct |
5 |
Correct |
4 ms |
12628 KB |
Output is correct |
6 |
Correct |
5 ms |
12628 KB |
Output is correct |
7 |
Correct |
4 ms |
12628 KB |
Output is correct |
8 |
Correct |
6 ms |
12884 KB |
Output is correct |
9 |
Correct |
6 ms |
12884 KB |
Output is correct |
10 |
Correct |
32 ms |
16724 KB |
Output is correct |
11 |
Correct |
68 ms |
17620 KB |
Output is correct |
12 |
Correct |
103 ms |
20044 KB |
Output is correct |
13 |
Correct |
115 ms |
20820 KB |
Output is correct |
14 |
Correct |
81 ms |
19020 KB |
Output is correct |
15 |
Correct |
54 ms |
16716 KB |
Output is correct |
16 |
Correct |
69 ms |
16724 KB |
Output is correct |
17 |
Correct |
310 ms |
17580 KB |
Output is correct |
18 |
Correct |
3 ms |
12628 KB |
Output is correct |
19 |
Correct |
3 ms |
12628 KB |
Output is correct |
20 |
Correct |
4 ms |
12628 KB |
Output is correct |
21 |
Correct |
3 ms |
12628 KB |
Output is correct |
22 |
Correct |
3 ms |
12628 KB |
Output is correct |
23 |
Correct |
9 ms |
12628 KB |
Output is correct |
24 |
Correct |
12 ms |
12628 KB |
Output is correct |
25 |
Correct |
15 ms |
12960 KB |
Output is correct |
26 |
Correct |
14 ms |
12916 KB |
Output is correct |
27 |
Correct |
40 ms |
16804 KB |
Output is correct |
28 |
Correct |
467 ms |
20056 KB |
Output is correct |
29 |
Correct |
476 ms |
20824 KB |
Output is correct |
30 |
Correct |
468 ms |
19032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
15976 KB |
Output is correct |
2 |
Correct |
4 ms |
12628 KB |
Output is correct |
3 |
Correct |
4 ms |
12624 KB |
Output is correct |
4 |
Correct |
4 ms |
12624 KB |
Output is correct |
5 |
Correct |
6 ms |
12624 KB |
Output is correct |
6 |
Correct |
6 ms |
12792 KB |
Output is correct |
7 |
Correct |
6 ms |
12624 KB |
Output is correct |
8 |
Correct |
50 ms |
16120 KB |
Output is correct |
9 |
Correct |
58 ms |
15848 KB |
Output is correct |
10 |
Correct |
21 ms |
12880 KB |
Output is correct |
11 |
Correct |
79 ms |
16200 KB |
Output is correct |
12 |
Correct |
4 ms |
12624 KB |
Output is correct |
13 |
Correct |
5 ms |
12624 KB |
Output is correct |
14 |
Correct |
3 ms |
12676 KB |
Output is correct |
15 |
Correct |
4 ms |
12624 KB |
Output is correct |
16 |
Correct |
3 ms |
12624 KB |
Output is correct |
17 |
Correct |
22 ms |
12796 KB |
Output is correct |
18 |
Correct |
20 ms |
12768 KB |
Output is correct |
19 |
Correct |
55 ms |
15952 KB |
Output is correct |
20 |
Correct |
108 ms |
18088 KB |
Output is correct |
21 |
Correct |
81 ms |
16168 KB |
Output is correct |
22 |
Correct |
78 ms |
15884 KB |
Output is correct |
23 |
Correct |
132 ms |
18248 KB |
Output is correct |
24 |
Correct |
64 ms |
16020 KB |
Output is correct |
25 |
Correct |
55 ms |
15444 KB |
Output is correct |
26 |
Correct |
55 ms |
15444 KB |
Output is correct |
27 |
Correct |
4 ms |
12624 KB |
Output is correct |
28 |
Correct |
4 ms |
12624 KB |
Output is correct |
29 |
Correct |
5 ms |
12628 KB |
Output is correct |
30 |
Correct |
5 ms |
12476 KB |
Output is correct |
31 |
Correct |
4 ms |
12628 KB |
Output is correct |
32 |
Correct |
5 ms |
12628 KB |
Output is correct |
33 |
Correct |
4 ms |
12628 KB |
Output is correct |
34 |
Correct |
6 ms |
12884 KB |
Output is correct |
35 |
Correct |
6 ms |
12884 KB |
Output is correct |
36 |
Correct |
32 ms |
16724 KB |
Output is correct |
37 |
Correct |
68 ms |
17620 KB |
Output is correct |
38 |
Correct |
103 ms |
20044 KB |
Output is correct |
39 |
Correct |
115 ms |
20820 KB |
Output is correct |
40 |
Correct |
81 ms |
19020 KB |
Output is correct |
41 |
Correct |
54 ms |
16716 KB |
Output is correct |
42 |
Correct |
69 ms |
16724 KB |
Output is correct |
43 |
Correct |
310 ms |
17580 KB |
Output is correct |
44 |
Correct |
3 ms |
12628 KB |
Output is correct |
45 |
Correct |
3 ms |
12628 KB |
Output is correct |
46 |
Correct |
4 ms |
12628 KB |
Output is correct |
47 |
Correct |
3 ms |
12628 KB |
Output is correct |
48 |
Correct |
3 ms |
12628 KB |
Output is correct |
49 |
Correct |
9 ms |
12628 KB |
Output is correct |
50 |
Correct |
12 ms |
12628 KB |
Output is correct |
51 |
Correct |
15 ms |
12960 KB |
Output is correct |
52 |
Correct |
14 ms |
12916 KB |
Output is correct |
53 |
Correct |
40 ms |
16804 KB |
Output is correct |
54 |
Correct |
467 ms |
20056 KB |
Output is correct |
55 |
Correct |
476 ms |
20824 KB |
Output is correct |
56 |
Correct |
468 ms |
19032 KB |
Output is correct |
57 |
Correct |
1706 ms |
21592 KB |
Output is correct |
58 |
Correct |
59 ms |
16972 KB |
Output is correct |
59 |
Correct |
977 ms |
17960 KB |
Output is correct |