#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
int n,m;
int d[5005],nr[5005];
bool sel[5005];
vector<pair<int,int>> G[5005];
void Bellman()
{
queue<int> q;
for(int i=0; i<=n; i++)
{
d[i] = oo;
sel[i] = false;
}
d[0] = 0;
sel[0] = true;
q.push(0);
nr[0] = 1;
while(!q.empty())
{
int nod = q.front();
q.pop();
sel[nod] = false;
for(auto it : G[nod])
{
if(d[nod] + it.second < d[it.first])
{
d[it.first] = d[nod] + it.second;
if(!sel[it.first])
{
q.push(it.first);
sel[it.first] = true;
}
nr[it.first] = nr[nod] + 1;
if(nr[it.first]>n || d[it.first]<0)
{
cout<<-1<<'\n';
return;
}
}
}
}
for(int i=0;i<=n;i++)
{
for(auto it : G[i])
{
if(d[i] + it.second < d[it.first])
{
cout<<-1<<'\n';
return;
}
}
}
for(int i=1;i<=n;i++)
{
cout<<d[i] - d[i-1]<<' ';
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++)
{
G[i-1].push_back({i,1});
G[i].push_back({i-1,0});
}
for(int i=1; i<=m; i++)
{
int l,r,k,val;
cin>>l>>r>>k>>val;
++l;
++r;
if(val==0)
{
G[l-1].push_back({r,(r - l + 1) - k});
}
else
{
G[r].push_back({l-1,-((r - l + 1) - (k - 1))});
}
}
Bellman();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
448 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
844 KB |
Output is correct |
2 |
Correct |
13 ms |
976 KB |
Output is correct |
3 |
Correct |
12 ms |
852 KB |
Output is correct |
4 |
Correct |
14 ms |
824 KB |
Output is correct |
5 |
Correct |
5 ms |
804 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
844 KB |
Output is correct |
2 |
Correct |
13 ms |
976 KB |
Output is correct |
3 |
Correct |
12 ms |
852 KB |
Output is correct |
4 |
Correct |
14 ms |
824 KB |
Output is correct |
5 |
Correct |
5 ms |
804 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
12 ms |
852 KB |
Output is correct |
8 |
Correct |
11 ms |
852 KB |
Output is correct |
9 |
Correct |
18 ms |
852 KB |
Output is correct |
10 |
Correct |
10 ms |
864 KB |
Output is correct |
11 |
Correct |
4 ms |
800 KB |
Output is correct |
12 |
Correct |
4 ms |
980 KB |
Output is correct |
13 |
Correct |
11 ms |
976 KB |
Output is correct |
14 |
Correct |
19 ms |
1008 KB |
Output is correct |
15 |
Correct |
31 ms |
1000 KB |
Output is correct |
16 |
Correct |
129 ms |
1004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
448 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
11 ms |
844 KB |
Output is correct |
12 |
Correct |
13 ms |
976 KB |
Output is correct |
13 |
Correct |
12 ms |
852 KB |
Output is correct |
14 |
Correct |
14 ms |
824 KB |
Output is correct |
15 |
Correct |
5 ms |
804 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
12 ms |
852 KB |
Output is correct |
18 |
Correct |
11 ms |
852 KB |
Output is correct |
19 |
Correct |
18 ms |
852 KB |
Output is correct |
20 |
Correct |
10 ms |
864 KB |
Output is correct |
21 |
Correct |
4 ms |
800 KB |
Output is correct |
22 |
Correct |
4 ms |
980 KB |
Output is correct |
23 |
Correct |
11 ms |
976 KB |
Output is correct |
24 |
Correct |
19 ms |
1008 KB |
Output is correct |
25 |
Correct |
31 ms |
1000 KB |
Output is correct |
26 |
Correct |
129 ms |
1004 KB |
Output is correct |
27 |
Correct |
8 ms |
996 KB |
Output is correct |
28 |
Correct |
9 ms |
980 KB |
Output is correct |
29 |
Correct |
6 ms |
980 KB |
Output is correct |
30 |
Correct |
7 ms |
1004 KB |
Output is correct |
31 |
Correct |
6 ms |
980 KB |
Output is correct |
32 |
Correct |
7 ms |
980 KB |
Output is correct |
33 |
Correct |
4 ms |
980 KB |
Output is correct |
34 |
Correct |
4 ms |
980 KB |
Output is correct |
35 |
Correct |
7 ms |
980 KB |
Output is correct |
36 |
Correct |
8 ms |
1004 KB |
Output is correct |