#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
using vi = vector<int>;
using ll = long long;
using vl = vector<ll>;
const int mx = 5'000;
int N, M;
vi T(mx), L(mx), R(mx), C(mx);
int lw, rw;
vi edge[mx + 2];
vl wt[mx + 2];
void add_edge(int u, int v, ll w)
{
edge[u].push_back(v);
// edge[v].push_back(u); //check this later???
wt[u].push_back(w);
// wt[v].push_back(w);
// cerr << "adding edge " << u << " -> " << v << '\n';
}
ll INF = 1'000'000'000'000'000'000LL;
vl lw_dist(mx+2, INF);
struct pos
{
int i;
};
bool operator < (pos A, pos B)
{
if(lw_dist[A.i] == lw_dist[B.i]) return A.i < B.i;
return lw_dist[A.i] < lw_dist[B.i];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for(int i = 0; i < M; i++)
{
cin >> T[i] >> L[i] >> R[i] >> C[i];
}
lw = M;
rw = M + 1;
for(int i = 0; i < M; i++)
{
for(int j = 0; j < M; j++)
{
if(j == i) continue;
if(T[j] >= T[i])
{
// cerr << "part 1\n";
if(L[j] <= R[i] + 1 - abs(T[j] - T[i]) && R[i] + 1 - abs(T[j] - T[i]) <= R[j])
add_edge(i, j, C[i]);
// cerr << "part 2\n";
if(L[j] <= L[i] - 1 + abs(T[j] - T[i]) && L[i] - 1 + abs(T[j] - T[i]) <= R[j])
add_edge(j, i, C[j]);
}
}
}
for(int i = 0; i < M; i++)
{
if(L[i] == 1) add_edge(lw, i, 0);
if(R[i] == N) add_edge(i, rw, C[i]);
}
lw_dist[lw] = 0;
// cerr << "check\n";
set<pos> S;
for(int i = 0; i < M+2; i++) S.insert(pos{i});
while(!S.empty())
{
int u = S.begin()->i;
S.erase(S.begin());
for(int e = 0; e < int(edge[u].size()); e++)
{
int v = edge[u][e];
ll w = wt[u][e];
if(lw_dist[u] + w < lw_dist[v])
{
S.erase(pos{v});
lw_dist[v] = lw_dist[u] + w;
S.insert(pos{v});
}
}
}
if(lw_dist[rw] == INF) lw_dist[rw] = -1;
cout << lw_dist[rw] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
2980 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
0 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
672 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
672 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
0 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
0 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
0 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
672 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
672 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
0 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
0 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
676 KB |
Output is correct |
20 |
Correct |
315 ms |
103052 KB |
Output is correct |
21 |
Correct |
327 ms |
102860 KB |
Output is correct |
22 |
Correct |
109 ms |
2116 KB |
Output is correct |
23 |
Correct |
108 ms |
2188 KB |
Output is correct |
24 |
Correct |
155 ms |
51468 KB |
Output is correct |
25 |
Correct |
104 ms |
6824 KB |
Output is correct |
26 |
Correct |
54 ms |
1732 KB |
Output is correct |
27 |
Correct |
47 ms |
1304 KB |
Output is correct |
28 |
Correct |
151 ms |
51652 KB |
Output is correct |
29 |
Correct |
71 ms |
6592 KB |
Output is correct |
30 |
Correct |
50 ms |
1836 KB |
Output is correct |
31 |
Correct |
47 ms |
1092 KB |
Output is correct |
32 |
Correct |
366 ms |
121700 KB |
Output is correct |
33 |
Correct |
389 ms |
159044 KB |
Output is correct |
34 |
Correct |
149 ms |
3240 KB |
Output is correct |
35 |
Correct |
360 ms |
121636 KB |
Output is correct |
36 |
Correct |
393 ms |
158084 KB |
Output is correct |
37 |
Correct |
154 ms |
3268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
2980 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |