This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int oo = 1e15;
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];
}
if(nr[it.first]>n)
{
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]<<' ';
}
}
signed 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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |