forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnikola.cpp
50 lines (41 loc) · 1.11 KB
/
nikola.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = (ll)1 << 60;
int main() {
int n;
cin >> n;
vector<ll> cost(n);
for(auto& i : cost) {
cin >> i;
}
vector<vector<ll>> dist;
dist.resize(n, vector<ll>(n+1, inf));
priority_queue<pair<ll, pair<int,int>>> q;
q.push({0, {1,1}});
dist[0][0] = 0;
dist[1][1] = cost[1];
while(!q.empty()) {
int curr = q.top().second.first;
int time = q.top().second.second;
q.pop();
if(curr == n-1) {
cout << dist[curr][time] << endl;
break;
}
int next = curr - time;
if(next >= 0) {
if(dist[next][time] == inf) {
dist[next][time] = dist[curr][time] + cost[next];
q.push({-dist[next][time], {next, time}});
}
}
next = curr + time + 1;
if(next < n) {
if(dist[next][time+1] == inf) {
dist[next][time+1] = dist[curr][time] + cost[next];
q.push({-dist[next][time+1], {next, time+1}});
}
}
}
}