#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>pp;
const int MAX=200000;
int n,m,inp[MAX][4],outp[MAX];
bool vis[MAX];
unordered_set<int>f[MAX],b[MAX];
queue<int>q;
priority_queue<pp>pq;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(vis,false,sizeof(vis));
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>inp[i][0]>>inp[i][1]>>inp[i][2]>>inp[i][3];
inp[i][0]--;
inp[i][1]--;
f[inp[i][0]].insert(i);
b[inp[i][1]].insert(i);
}
for(int i=0;i<n;i++)if(f[i].size()==0)q.push(i);
while(!q.empty()){
int cur=q.front();
q.pop();
outp[cur]=-1;
for(auto&i:b[cur]){
if(vis[i])continue;
vis[i]=true;
f[inp[i][0]].erase(f[inp[i][0]].find(i));
if(f[inp[i][0]].size()==0)q.push(inp[i][0]);
}
}
for(int i=0;i<m;i++)if(!vis[i])pq.push({inp[i][2],i});
while(!pq.empty()){
pp cur=pq.top();
pq.pop();
if(vis[cur.second])continue;
vis[cur.second]=true;
int ai=inp[cur.second][0];
int bi=inp[cur.second][1];
f[ai].erase(f[ai].find(cur.second));
b[bi].erase(b[bi].find(cur.second));
if(f[ai].size()==0){
for(auto&i:b[ai])if(!vis[i])if(cur.first-inp[i][3]>inp[i][2])pq.push({cur.first-inp[i][3],i});
outp[ai]=cur.first;
}
}
for(int i=0;i<n-1;i++)cout<<outp[i]<<" ";
cout<<outp[n-1]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
22828 KB |
Output is correct |
2 |
Correct |
18 ms |
22780 KB |
Output is correct |
3 |
Correct |
17 ms |
22612 KB |
Output is correct |
4 |
Correct |
17 ms |
22732 KB |
Output is correct |
5 |
Correct |
18 ms |
22828 KB |
Output is correct |
6 |
Correct |
18 ms |
22788 KB |
Output is correct |
7 |
Correct |
17 ms |
22628 KB |
Output is correct |
8 |
Correct |
19 ms |
23064 KB |
Output is correct |
9 |
Correct |
18 ms |
22920 KB |
Output is correct |
10 |
Correct |
17 ms |
22592 KB |
Output is correct |
11 |
Correct |
21 ms |
22732 KB |
Output is correct |
12 |
Correct |
17 ms |
22672 KB |
Output is correct |
13 |
Correct |
18 ms |
22604 KB |
Output is correct |
14 |
Correct |
20 ms |
22988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
531 ms |
67596 KB |
Output is correct |
2 |
Correct |
535 ms |
67568 KB |
Output is correct |
3 |
Correct |
472 ms |
45436 KB |
Output is correct |
4 |
Correct |
48 ms |
25744 KB |
Output is correct |
5 |
Correct |
536 ms |
62980 KB |
Output is correct |
6 |
Correct |
578 ms |
62976 KB |
Output is correct |
7 |
Correct |
516 ms |
44380 KB |
Output is correct |
8 |
Correct |
587 ms |
86304 KB |
Output is correct |
9 |
Correct |
605 ms |
77528 KB |
Output is correct |
10 |
Correct |
395 ms |
43556 KB |
Output is correct |
11 |
Correct |
534 ms |
59580 KB |
Output is correct |
12 |
Correct |
408 ms |
43448 KB |
Output is correct |
13 |
Correct |
252 ms |
46508 KB |
Output is correct |
14 |
Correct |
603 ms |
86364 KB |
Output is correct |
15 |
Correct |
594 ms |
86440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
22828 KB |
Output is correct |
2 |
Correct |
18 ms |
22780 KB |
Output is correct |
3 |
Correct |
17 ms |
22612 KB |
Output is correct |
4 |
Correct |
17 ms |
22732 KB |
Output is correct |
5 |
Correct |
18 ms |
22828 KB |
Output is correct |
6 |
Correct |
18 ms |
22788 KB |
Output is correct |
7 |
Correct |
17 ms |
22628 KB |
Output is correct |
8 |
Correct |
19 ms |
23064 KB |
Output is correct |
9 |
Correct |
18 ms |
22920 KB |
Output is correct |
10 |
Correct |
17 ms |
22592 KB |
Output is correct |
11 |
Correct |
21 ms |
22732 KB |
Output is correct |
12 |
Correct |
17 ms |
22672 KB |
Output is correct |
13 |
Correct |
18 ms |
22604 KB |
Output is correct |
14 |
Correct |
20 ms |
22988 KB |
Output is correct |
15 |
Correct |
531 ms |
67596 KB |
Output is correct |
16 |
Correct |
535 ms |
67568 KB |
Output is correct |
17 |
Correct |
472 ms |
45436 KB |
Output is correct |
18 |
Correct |
48 ms |
25744 KB |
Output is correct |
19 |
Correct |
536 ms |
62980 KB |
Output is correct |
20 |
Correct |
578 ms |
62976 KB |
Output is correct |
21 |
Correct |
516 ms |
44380 KB |
Output is correct |
22 |
Correct |
587 ms |
86304 KB |
Output is correct |
23 |
Correct |
605 ms |
77528 KB |
Output is correct |
24 |
Correct |
395 ms |
43556 KB |
Output is correct |
25 |
Correct |
534 ms |
59580 KB |
Output is correct |
26 |
Correct |
408 ms |
43448 KB |
Output is correct |
27 |
Correct |
252 ms |
46508 KB |
Output is correct |
28 |
Correct |
603 ms |
86364 KB |
Output is correct |
29 |
Correct |
594 ms |
86440 KB |
Output is correct |
30 |
Correct |
542 ms |
67348 KB |
Output is correct |
31 |
Correct |
557 ms |
67464 KB |
Output is correct |
32 |
Correct |
494 ms |
45472 KB |
Output is correct |
33 |
Correct |
50 ms |
25832 KB |
Output is correct |
34 |
Correct |
528 ms |
63076 KB |
Output is correct |
35 |
Correct |
535 ms |
62980 KB |
Output is correct |
36 |
Correct |
514 ms |
44400 KB |
Output is correct |
37 |
Correct |
600 ms |
86312 KB |
Output is correct |
38 |
Correct |
654 ms |
76988 KB |
Output is correct |
39 |
Correct |
455 ms |
43516 KB |
Output is correct |
40 |
Correct |
597 ms |
59600 KB |
Output is correct |
41 |
Correct |
341 ms |
43460 KB |
Output is correct |
42 |
Correct |
260 ms |
46544 KB |
Output is correct |
43 |
Correct |
618 ms |
86324 KB |
Output is correct |
44 |
Correct |
604 ms |
86368 KB |
Output is correct |
45 |
Correct |
594 ms |
86400 KB |
Output is correct |
46 |
Correct |
458 ms |
67020 KB |
Output is correct |
47 |
Correct |
490 ms |
65152 KB |
Output is correct |
48 |
Correct |
469 ms |
65288 KB |
Output is correct |
49 |
Correct |
513 ms |
65360 KB |
Output is correct |
50 |
Correct |
15 ms |
22348 KB |
Output is correct |