Submission #1102437

# Submission time Handle Problem Language Result Execution time Memory
1102437 2024-10-18T06:46:36 Z vjudge1 Travelling Merchant (CCO21_day2problem1) C++17
25 / 25
113 ms 23016 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))

mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 2e5 + 5;

struct Edge
{
    int u, v, r, p;
    bool operator<(const Edge& o)
    {
        return r < o.r;
    }
}E[NM];

vector<int> g[NM];
int n, m, deg[NM], ans[NM];
bool ok[NM];

void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> E[i].u >> E[i].v >> E[i].r >> E[i].p;
    sort(E + 1, E + 1 + m);
    for (int i = 1; i <= m; i++)
    {
        g[E[i].v].push_back(i);
        deg[E[i].u]++;
    }
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        ans[i] = -2;
        if (!deg[i])
            q.push(i);
    }
    while (q.size())
    {
        int u = q.front();
        q.pop();
        ans[u] = -1;
        for (int v : g[u])
        {
            deg[E[v].u]--;
            ok[v] = 1;
            if (!deg[E[v].u])
                q.push(E[v].u);
        }
    }
    for (int i = m; i >= 1; i--)
        if (!ok[i])
        {
            ok[i] = 1;
            deg[E[i].u]--;
            if (ans[E[i].u] == -2)
                ans[E[i].u] = E[i].r;
            else
                setmin(ans[E[i].u], E[i].r);
            if (!deg[E[i].u])
            {
                q.push(E[i].u);
                while (q.size())
                {
                    int u = q.front();
                    q.pop();
                    for (int v : g[u])
                        if (!ok[v])
                        {
                            if (ans[E[v].u] == -2)
                                ans[E[v].u] = max(ans[u] - E[v].p, E[v].r);
                            else
                                setmin(ans[E[v].u], max(ans[u] - E[v].p, E[v].r));
                            deg[E[v].u]--;
                            ok[v] = 1;
                            if (!deg[E[v].u])
                                q.push(E[v].u);
                        }
                }
            }
        }
    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    int tc = 1; 
    // cin >> tc;
    while (tc--)
        solve();
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 3 ms 8528 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8700 KB Output is correct
5 Correct 4 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8696 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 3 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 3 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 96 ms 20180 KB Output is correct
2 Correct 87 ms 20040 KB Output is correct
3 Correct 56 ms 17240 KB Output is correct
4 Correct 16 ms 10844 KB Output is correct
5 Correct 76 ms 19528 KB Output is correct
6 Correct 91 ms 19528 KB Output is correct
7 Correct 56 ms 16868 KB Output is correct
8 Correct 98 ms 23004 KB Output is correct
9 Correct 113 ms 21832 KB Output is correct
10 Correct 51 ms 16708 KB Output is correct
11 Correct 72 ms 18772 KB Output is correct
12 Correct 38 ms 16968 KB Output is correct
13 Correct 38 ms 16472 KB Output is correct
14 Correct 92 ms 22856 KB Output is correct
15 Correct 86 ms 22976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 3 ms 8528 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 2 ms 8700 KB Output is correct
5 Correct 4 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8696 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 3 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 3 ms 8528 KB Output is correct
15 Correct 96 ms 20180 KB Output is correct
16 Correct 87 ms 20040 KB Output is correct
17 Correct 56 ms 17240 KB Output is correct
18 Correct 16 ms 10844 KB Output is correct
19 Correct 76 ms 19528 KB Output is correct
20 Correct 91 ms 19528 KB Output is correct
21 Correct 56 ms 16868 KB Output is correct
22 Correct 98 ms 23004 KB Output is correct
23 Correct 113 ms 21832 KB Output is correct
24 Correct 51 ms 16708 KB Output is correct
25 Correct 72 ms 18772 KB Output is correct
26 Correct 38 ms 16968 KB Output is correct
27 Correct 38 ms 16472 KB Output is correct
28 Correct 92 ms 22856 KB Output is correct
29 Correct 86 ms 22976 KB Output is correct
30 Correct 106 ms 20024 KB Output is correct
31 Correct 113 ms 20024 KB Output is correct
32 Correct 50 ms 17480 KB Output is correct
33 Correct 16 ms 10832 KB Output is correct
34 Correct 82 ms 19436 KB Output is correct
35 Correct 85 ms 19540 KB Output is correct
36 Correct 60 ms 16968 KB Output is correct
37 Correct 96 ms 22856 KB Output is correct
38 Correct 102 ms 21320 KB Output is correct
39 Correct 56 ms 16736 KB Output is correct
40 Correct 78 ms 18760 KB Output is correct
41 Correct 43 ms 16928 KB Output is correct
42 Correct 46 ms 16316 KB Output is correct
43 Correct 92 ms 22856 KB Output is correct
44 Correct 82 ms 23016 KB Output is correct
45 Correct 88 ms 22856 KB Output is correct
46 Correct 67 ms 19016 KB Output is correct
47 Correct 70 ms 18760 KB Output is correct
48 Correct 67 ms 18772 KB Output is correct
49 Correct 74 ms 18760 KB Output is correct
50 Correct 2 ms 8528 KB Output is correct