#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1000000000;
constexpr int NMAX = 5005;
typedef pair <int, int> PII;
int N, M;
vector <PII> G[NMAX];
int sp[NMAX];
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
for (int i = 1; i <= M; ++ i ) {
int L, R, K, Val;
cin >> L >> R >> K >> Val;
++ L;
++ R;
int Lg = R - L + 1;
if (Val == 0) {
G[L-1].push_back({Lg-K, R});
}
else G[R].push_back({-Lg+K-1, L-1});
}
for (int i = 0; i <= N; ++ i )
sp[i] = INF;
for (int i = 1; i <= N; ++ i ) {
int L = i, R = i;
G[R].push_back({0, L-1});
G[L-1].push_back({1, R});
}
}
bool InQueue[NMAX];
int length[NMAX];
bool sol;
void BFS () {
queue <int> Q;
Q.push(0);
InQueue[0] = 1;
sp[0] = 0;
length[0] = 1;
while (!Q.empty()) {
int nod = Q.front();
Q.pop();
InQueue[nod] = 0;
for (auto it : G[nod]) {
int to = it.second;
int cost = it.first;
if (sp[nod] + cost < sp[to]) {
sp[to] = sp[nod] + cost;
length[to] = length[nod] + 1;
if (length[to] > N) {
cout << -1 << '\n';
return;
}
if (!InQueue[to]) {
Q.push(to);
InQueue[to] = 1;
}
}
}
}
for (int i = 1; i <= N; ++ i )
cout << sp[i] - sp[i-1] << " ";
}
int main () {
Read();
BFS();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
716 KB |
Output is correct |
2 |
Correct |
12 ms |
692 KB |
Output is correct |
3 |
Correct |
11 ms |
768 KB |
Output is correct |
4 |
Correct |
10 ms |
772 KB |
Output is correct |
5 |
Correct |
367 ms |
804 KB |
Output is correct |
6 |
Correct |
392 ms |
788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
716 KB |
Output is correct |
2 |
Correct |
12 ms |
692 KB |
Output is correct |
3 |
Correct |
11 ms |
768 KB |
Output is correct |
4 |
Correct |
10 ms |
772 KB |
Output is correct |
5 |
Correct |
367 ms |
804 KB |
Output is correct |
6 |
Correct |
392 ms |
788 KB |
Output is correct |
7 |
Correct |
15 ms |
816 KB |
Output is correct |
8 |
Correct |
12 ms |
796 KB |
Output is correct |
9 |
Correct |
17 ms |
824 KB |
Output is correct |
10 |
Correct |
10 ms |
820 KB |
Output is correct |
11 |
Execution timed out |
995 ms |
812 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
11 ms |
716 KB |
Output is correct |
12 |
Correct |
12 ms |
692 KB |
Output is correct |
13 |
Correct |
11 ms |
768 KB |
Output is correct |
14 |
Correct |
10 ms |
772 KB |
Output is correct |
15 |
Correct |
367 ms |
804 KB |
Output is correct |
16 |
Correct |
392 ms |
788 KB |
Output is correct |
17 |
Correct |
15 ms |
816 KB |
Output is correct |
18 |
Correct |
12 ms |
796 KB |
Output is correct |
19 |
Correct |
17 ms |
824 KB |
Output is correct |
20 |
Correct |
10 ms |
820 KB |
Output is correct |
21 |
Execution timed out |
995 ms |
812 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |