#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template <typename T>
int sz(const T &a){return int(a.size());}
const int MN=2e5+1;
vector<pair<int,pll>> adj[MN];
bool gone[MN];
ll tried[MN];
ll work[MN];
bool dfs(int loc, ll am){
if(am<=tried[loc])return false;
if(am>=work[loc])return true;
gone[loc]=true;
for(auto x:adj[loc]){
if(x.second.first<=am){
if(gone[x.first]){
gone[loc]=false;
work[loc]=am;
return true;
}
if(dfs(x.first,am+x.second.second)){
gone[loc]=false;
work[loc]=am;
return true;
}
}
}
tried[loc]=am;
gone[loc]=false;
return false;
}
int main(int argc, char *argv[]) {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n,m,a,b;
cin>>n>>m;
ll r,p;
for(int i=0;i<m;i++){
cin>>a>>b>>r>>p;
adj[a].push_back({b,{r,p}});
}
for(int i=1;i<=n;i++)tried[i]=-1,work[i]=LLONG_MAX;
for(int i=1;i<=n;i++){
ll lo=0,hi=1e9;
while(lo!=hi){
ll mid=(lo+hi)/2;
if(dfs(i,mid))hi=mid;
else lo=mid+1;
}
if(lo==1e9)printf("-1%c"," \n"[i==n]);
else printf("%lli%c",lo," \n"[i==n]);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5136 KB |
Output is correct |
2 |
Correct |
7 ms |
5144 KB |
Output is correct |
3 |
Correct |
5 ms |
5068 KB |
Output is correct |
4 |
Correct |
5 ms |
5068 KB |
Output is correct |
5 |
Correct |
5 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5068 KB |
Output is correct |
8 |
Correct |
6 ms |
5196 KB |
Output is correct |
9 |
Correct |
6 ms |
5136 KB |
Output is correct |
10 |
Correct |
5 ms |
5068 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5068 KB |
Output is correct |
14 |
Correct |
7 ms |
5196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
256 ms |
15468 KB |
Output is correct |
2 |
Correct |
263 ms |
15280 KB |
Output is correct |
3 |
Correct |
81 ms |
12232 KB |
Output is correct |
4 |
Correct |
74 ms |
9284 KB |
Output is correct |
5 |
Correct |
250 ms |
14240 KB |
Output is correct |
6 |
Correct |
214 ms |
14128 KB |
Output is correct |
7 |
Correct |
82 ms |
11556 KB |
Output is correct |
8 |
Correct |
1394 ms |
29020 KB |
Output is correct |
9 |
Correct |
360 ms |
16424 KB |
Output is correct |
10 |
Correct |
76 ms |
10236 KB |
Output is correct |
11 |
Correct |
224 ms |
13936 KB |
Output is correct |
12 |
Correct |
67 ms |
10620 KB |
Output is correct |
13 |
Correct |
64 ms |
10032 KB |
Output is correct |
14 |
Correct |
1411 ms |
29048 KB |
Output is correct |
15 |
Correct |
1402 ms |
29000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5136 KB |
Output is correct |
2 |
Correct |
7 ms |
5144 KB |
Output is correct |
3 |
Correct |
5 ms |
5068 KB |
Output is correct |
4 |
Correct |
5 ms |
5068 KB |
Output is correct |
5 |
Correct |
5 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5068 KB |
Output is correct |
8 |
Correct |
6 ms |
5196 KB |
Output is correct |
9 |
Correct |
6 ms |
5136 KB |
Output is correct |
10 |
Correct |
5 ms |
5068 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5068 KB |
Output is correct |
14 |
Correct |
7 ms |
5196 KB |
Output is correct |
15 |
Correct |
256 ms |
15468 KB |
Output is correct |
16 |
Correct |
263 ms |
15280 KB |
Output is correct |
17 |
Correct |
81 ms |
12232 KB |
Output is correct |
18 |
Correct |
74 ms |
9284 KB |
Output is correct |
19 |
Correct |
250 ms |
14240 KB |
Output is correct |
20 |
Correct |
214 ms |
14128 KB |
Output is correct |
21 |
Correct |
82 ms |
11556 KB |
Output is correct |
22 |
Correct |
1394 ms |
29020 KB |
Output is correct |
23 |
Correct |
360 ms |
16424 KB |
Output is correct |
24 |
Correct |
76 ms |
10236 KB |
Output is correct |
25 |
Correct |
224 ms |
13936 KB |
Output is correct |
26 |
Correct |
67 ms |
10620 KB |
Output is correct |
27 |
Correct |
64 ms |
10032 KB |
Output is correct |
28 |
Correct |
1411 ms |
29048 KB |
Output is correct |
29 |
Correct |
1402 ms |
29000 KB |
Output is correct |
30 |
Correct |
273 ms |
15308 KB |
Output is correct |
31 |
Correct |
278 ms |
15352 KB |
Output is correct |
32 |
Correct |
73 ms |
12052 KB |
Output is correct |
33 |
Correct |
60 ms |
9204 KB |
Output is correct |
34 |
Correct |
189 ms |
14108 KB |
Output is correct |
35 |
Correct |
205 ms |
14172 KB |
Output is correct |
36 |
Correct |
89 ms |
11644 KB |
Output is correct |
37 |
Correct |
1412 ms |
29068 KB |
Output is correct |
38 |
Correct |
471 ms |
15972 KB |
Output is correct |
39 |
Correct |
77 ms |
10000 KB |
Output is correct |
40 |
Correct |
230 ms |
13952 KB |
Output is correct |
41 |
Correct |
67 ms |
10680 KB |
Output is correct |
42 |
Correct |
64 ms |
9740 KB |
Output is correct |
43 |
Correct |
1397 ms |
29076 KB |
Output is correct |
44 |
Correct |
1321 ms |
29112 KB |
Output is correct |
45 |
Correct |
1308 ms |
29016 KB |
Output is correct |
46 |
Correct |
1897 ms |
18484 KB |
Output is correct |
47 |
Correct |
516 ms |
15040 KB |
Output is correct |
48 |
Correct |
181 ms |
13160 KB |
Output is correct |
49 |
Correct |
545 ms |
17920 KB |
Output is correct |
50 |
Correct |
4 ms |
4940 KB |
Output is correct |