#include <bits/stdc++.h>
#define NMAX ((int)5e3)
#define INF ((int)1e9)
using namespace std;
struct edge {
int dest, cost;
};
int n, m;
vector<edge> a[NMAX + 1];
int dist[NMAX + 1];
bool bellmanford() {
queue <int> Q;
bitset <NMAX> in_queue(false);
vector <int> cnt_in_queue(NMAX, 0);
int negative_cycle = false;
for (int i = 1; i <= n; ++i) {
dist[i] = INF;
}
dist[0] = 0, Q.push(0), in_queue[0].flip();
while (!Q.empty() && !negative_cycle) {
int x = Q.front();
Q.pop();
in_queue[x] = false;
for (auto it = a[x].begin(); it != a[x].end(); ++ it) if (dist[x] < INF) {
edge &next = *it;
if (dist[next.dest] > dist[x] + next.cost) {
dist[next.dest] = dist[x] + next.cost;
if (!in_queue[next.dest]) {
if (cnt_in_queue[next.dest] > n)
return false;
else {
Q.push(next.dest);
in_queue[next.dest] = true;
cnt_in_queue[next.dest] ++;
}
}
}
}
}
return true;
}
void add_constraint(int left, int right, int k, int val) {
if (val == 0) {
a[left - 1].push_back({right, (right - left + 1) - k});
} else {
a[right].push_back({left - 1, -((right - left + 1) - (k - 1))});
}
}
int main(void) {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; ++i) {
int left, right, k, val;
scanf("%d %d %d %d", &left, &right, &k, &val);
++left; ++right;
add_constraint(left, right, k, val);
}
for (int i = 1; i <= n; ++i) {
a[i - 1].push_back({i, 1});
a[i].push_back({i - 1, 0});
}
if (!bellmanford()) {
printf("-1\n");
return 0;
}
for (int i = 1; i <= n; ++i) {
printf("%d ", dist[i] - dist[i - 1]);
}
printf("\n");
return 0;
}
Compilation message
restore.cpp: In function 'int main()':
restore.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
restore.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d %d %d %d", &left, &right, &k, &val);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
724 KB |
Output is correct |
2 |
Correct |
13 ms |
704 KB |
Output is correct |
3 |
Correct |
13 ms |
748 KB |
Output is correct |
4 |
Correct |
13 ms |
724 KB |
Output is correct |
5 |
Execution timed out |
1097 ms |
724 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
724 KB |
Output is correct |
2 |
Correct |
13 ms |
704 KB |
Output is correct |
3 |
Correct |
13 ms |
748 KB |
Output is correct |
4 |
Correct |
13 ms |
724 KB |
Output is correct |
5 |
Execution timed out |
1097 ms |
724 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
12 ms |
724 KB |
Output is correct |
12 |
Correct |
13 ms |
704 KB |
Output is correct |
13 |
Correct |
13 ms |
748 KB |
Output is correct |
14 |
Correct |
13 ms |
724 KB |
Output is correct |
15 |
Execution timed out |
1097 ms |
724 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |