Submission #1102558

# Submission time Handle Problem Language Result Execution time Memory
1102558 2024-10-18T09:31:46 Z daoquanglinh2007 Travelling Merchant (CCO21_day2problem1) C++17
25 / 25
135 ms 24568 KB
#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;
}
# Verdict Execution time Memory 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 14756 KB Output is correct
7 Correct 3 ms 14684 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 2 ms 14672 KB Output is correct
13 Correct 2 ms 14672 KB Output is correct
14 Correct 3 ms 14928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 21548 KB Output is correct
2 Correct 96 ms 21576 KB Output is correct
3 Correct 66 ms 18776 KB Output is correct
4 Correct 18 ms 17400 KB Output is correct
5 Correct 92 ms 20808 KB Output is correct
6 Correct 89 ms 20808 KB Output is correct
7 Correct 63 ms 18248 KB Output is correct
8 Correct 118 ms 24392 KB Output is correct
9 Correct 93 ms 23368 KB Output is correct
10 Correct 57 ms 18044 KB Output is correct
11 Correct 77 ms 20268 KB Output is correct
12 Correct 41 ms 18504 KB Output is correct
13 Correct 39 ms 17856 KB Output is correct
14 Correct 131 ms 24392 KB Output is correct
15 Correct 128 ms 24392 KB Output is correct
# Verdict Execution time Memory 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 14756 KB Output is correct
7 Correct 3 ms 14684 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 2 ms 14672 KB Output is correct
13 Correct 2 ms 14672 KB Output is correct
14 Correct 3 ms 14928 KB Output is correct
15 Correct 118 ms 21548 KB Output is correct
16 Correct 96 ms 21576 KB Output is correct
17 Correct 66 ms 18776 KB Output is correct
18 Correct 18 ms 17400 KB Output is correct
19 Correct 92 ms 20808 KB Output is correct
20 Correct 89 ms 20808 KB Output is correct
21 Correct 63 ms 18248 KB Output is correct
22 Correct 118 ms 24392 KB Output is correct
23 Correct 93 ms 23368 KB Output is correct
24 Correct 57 ms 18044 KB Output is correct
25 Correct 77 ms 20268 KB Output is correct
26 Correct 41 ms 18504 KB Output is correct
27 Correct 39 ms 17856 KB Output is correct
28 Correct 131 ms 24392 KB Output is correct
29 Correct 128 ms 24392 KB Output is correct
30 Correct 105 ms 21576 KB Output is correct
31 Correct 100 ms 21736 KB Output is correct
32 Correct 52 ms 18760 KB Output is correct
33 Correct 18 ms 17248 KB Output is correct
34 Correct 85 ms 20808 KB Output is correct
35 Correct 83 ms 20808 KB Output is correct
36 Correct 63 ms 18248 KB Output is correct
37 Correct 135 ms 24392 KB Output is correct
38 Correct 131 ms 22848 KB Output is correct
39 Correct 57 ms 17828 KB Output is correct
40 Correct 80 ms 20304 KB Output is correct
41 Correct 42 ms 18248 KB Output is correct
42 Correct 47 ms 17732 KB Output is correct
43 Correct 124 ms 24392 KB Output is correct
44 Correct 106 ms 24568 KB Output is correct
45 Correct 132 ms 24420 KB Output is correct
46 Correct 83 ms 21064 KB Output is correct
47 Correct 72 ms 20300 KB Output is correct
48 Correct 80 ms 20296 KB Output is correct
49 Correct 73 ms 20296 KB Output is correct
50 Correct 2 ms 14672 KB Output is correct