#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define mp make_pair
const int NM = 2e5, inf = 1e18;
int N, M, u[NM+5], v[NM+5], r[NM+5], p[NM+5], od[NM+5];
vector <int> g[NM+5];
int deg[NM+5];
stack <int> st;
bool del[NM+5];
priority_queue <pair <pii, pii> > pq;
int ans[NM+5];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int i = 1; i <= M; i++){
cin >> u[i] >> v[i] >> r[i] >> p[i];
g[v[i]].push_back(i);
deg[u[i]]++;
od[i] = i;
}
sort(od+1, od+1+M, [&](int i, int j){
return r[i] > r[j];
});
for (int i = 1; i <= N; i++)
if (!deg[i]) st.push(i);
while (!st.empty()){
int x = st.top(); st.pop();
for (int i : g[x]){
del[i] = 1;
deg[u[i]]--;
if (!deg[u[i]]) st.push(u[i]);
}
}
for (int i = 1; i <= N; i++)
ans[i] = +inf;
for (int t = 1; t <= M; t++){
int i = od[t];
if (del[i]) continue;
del[i] = 1;
ans[u[i]] = min(ans[u[i]], r[i]);
deg[u[i]]--;
if (deg[u[i]]) continue;
st.push(u[i]);
while (!st.empty()){
int x = st.top(); st.pop();
for (int i : g[x]){
if (del[i]) continue;
del[i] = 1;
ans[u[i]] = min(ans[u[i]], max(ans[v[i]]-p[i], r[i]));
deg[u[i]]--;
if (!deg[u[i]]) st.push(u[i]);
}
}
}
for (int i = 1; i <= N; i++)
cout << (ans[i] == +inf ? -1 : ans[i]) << ' ';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14672 KB |
Output is correct |
2 |
Correct |
3 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
14672 KB |
Output is correct |
4 |
Correct |
3 ms |
14672 KB |
Output is correct |
5 |
Correct |
3 ms |
14672 KB |
Output is correct |
6 |
Correct |
3 ms |
14672 KB |
Output is correct |
7 |
Correct |
3 ms |
14672 KB |
Output is correct |
8 |
Correct |
3 ms |
14928 KB |
Output is correct |
9 |
Correct |
3 ms |
14672 KB |
Output is correct |
10 |
Correct |
3 ms |
14672 KB |
Output is correct |
11 |
Correct |
3 ms |
14672 KB |
Output is correct |
12 |
Correct |
3 ms |
14672 KB |
Output is correct |
13 |
Correct |
3 ms |
14672 KB |
Output is correct |
14 |
Correct |
3 ms |
14928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
21640 KB |
Output is correct |
2 |
Correct |
116 ms |
21576 KB |
Output is correct |
3 |
Correct |
60 ms |
18760 KB |
Output is correct |
4 |
Correct |
17 ms |
17232 KB |
Output is correct |
5 |
Correct |
93 ms |
20808 KB |
Output is correct |
6 |
Correct |
82 ms |
20808 KB |
Output is correct |
7 |
Correct |
56 ms |
18172 KB |
Output is correct |
8 |
Correct |
117 ms |
24392 KB |
Output is correct |
9 |
Correct |
112 ms |
23368 KB |
Output is correct |
10 |
Correct |
65 ms |
18044 KB |
Output is correct |
11 |
Correct |
90 ms |
20296 KB |
Output is correct |
12 |
Correct |
41 ms |
18512 KB |
Output is correct |
13 |
Correct |
41 ms |
17844 KB |
Output is correct |
14 |
Correct |
116 ms |
24400 KB |
Output is correct |
15 |
Correct |
109 ms |
24392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14672 KB |
Output is correct |
2 |
Correct |
3 ms |
14672 KB |
Output is correct |
3 |
Correct |
3 ms |
14672 KB |
Output is correct |
4 |
Correct |
3 ms |
14672 KB |
Output is correct |
5 |
Correct |
3 ms |
14672 KB |
Output is correct |
6 |
Correct |
3 ms |
14672 KB |
Output is correct |
7 |
Correct |
3 ms |
14672 KB |
Output is correct |
8 |
Correct |
3 ms |
14928 KB |
Output is correct |
9 |
Correct |
3 ms |
14672 KB |
Output is correct |
10 |
Correct |
3 ms |
14672 KB |
Output is correct |
11 |
Correct |
3 ms |
14672 KB |
Output is correct |
12 |
Correct |
3 ms |
14672 KB |
Output is correct |
13 |
Correct |
3 ms |
14672 KB |
Output is correct |
14 |
Correct |
3 ms |
14928 KB |
Output is correct |
15 |
Correct |
114 ms |
21640 KB |
Output is correct |
16 |
Correct |
116 ms |
21576 KB |
Output is correct |
17 |
Correct |
60 ms |
18760 KB |
Output is correct |
18 |
Correct |
17 ms |
17232 KB |
Output is correct |
19 |
Correct |
93 ms |
20808 KB |
Output is correct |
20 |
Correct |
82 ms |
20808 KB |
Output is correct |
21 |
Correct |
56 ms |
18172 KB |
Output is correct |
22 |
Correct |
117 ms |
24392 KB |
Output is correct |
23 |
Correct |
112 ms |
23368 KB |
Output is correct |
24 |
Correct |
65 ms |
18044 KB |
Output is correct |
25 |
Correct |
90 ms |
20296 KB |
Output is correct |
26 |
Correct |
41 ms |
18512 KB |
Output is correct |
27 |
Correct |
41 ms |
17844 KB |
Output is correct |
28 |
Correct |
116 ms |
24400 KB |
Output is correct |
29 |
Correct |
109 ms |
24392 KB |
Output is correct |
30 |
Correct |
101 ms |
21576 KB |
Output is correct |
31 |
Correct |
109 ms |
21684 KB |
Output is correct |
32 |
Correct |
52 ms |
18756 KB |
Output is correct |
33 |
Correct |
17 ms |
17176 KB |
Output is correct |
34 |
Correct |
92 ms |
20808 KB |
Output is correct |
35 |
Correct |
96 ms |
20824 KB |
Output is correct |
36 |
Correct |
64 ms |
18256 KB |
Output is correct |
37 |
Correct |
116 ms |
24392 KB |
Output is correct |
38 |
Correct |
97 ms |
22856 KB |
Output is correct |
39 |
Correct |
53 ms |
17980 KB |
Output is correct |
40 |
Correct |
85 ms |
20368 KB |
Output is correct |
41 |
Correct |
49 ms |
18232 KB |
Output is correct |
42 |
Correct |
35 ms |
17852 KB |
Output is correct |
43 |
Correct |
134 ms |
24392 KB |
Output is correct |
44 |
Correct |
135 ms |
24512 KB |
Output is correct |
45 |
Correct |
125 ms |
24392 KB |
Output is correct |
46 |
Correct |
60 ms |
21064 KB |
Output is correct |
47 |
Correct |
61 ms |
20364 KB |
Output is correct |
48 |
Correct |
70 ms |
20260 KB |
Output is correct |
49 |
Correct |
74 ms |
20296 KB |
Output is correct |
50 |
Correct |
2 ms |
14672 KB |
Output is correct |