1 条题解

  • 0
    @ 2025-4-11 22:44:39
    #include <bits/stdc++.h>
    using namespace std;
    int n, m;
    struct Edge
    {
        int a, b, r, p, id;
    };
    bool vis[212345]; // 标记每个id的边还在不在
    bool operator<(const Edge &x, const Edge &y)
    {
        return x.r < y.r;
    }
    priority_queue<Edge> pq;
    Edge e[212345];
    vector<Edge> ee[212345];
    vector<Edge> eee[212345];
    int inD[212345], outD[212345];
    queue<int> q;
    int ans[212345];
    int main()
    {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            ans[i] = 1000000001;
        for (int i = 1; i <= m; i++)
        {
            cin >> e[i].a >> e[i].b >> e[i].r >> e[i].p;
            e[i].id = i; // 用来标记每条边还在不在
            vis[i] = true;
    
            inD[e[i].b]++;
            outD[e[i].a]++;
            ee[e[i].a].push_back((Edge){e[i].a, e[i].b, e[i].r, e[i].p, e[i].id});
            // 反图
            eee[e[i].b].push_back((Edge){e[i].b, e[i].a, e[i].r, e[i].p, e[i].id});
        }
        // 处理出度为0的
        for (int i = 1; i <= n; i++)
            if (outD[i] == 0)
                q.push(i);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            ans[u] = -1;
            for (int i = 0; i < eee[u].size(); i++)
            {
                int v = eee[u][i].b;
                int id = eee[u][i].id;
                vis[id] = false;
                outD[v]--;
                if (outD[v] == 0)
                    q.push(v);
            }
        }
        // 剩下的边丢进优先队列 pq
        for (int i = 1; i <= m; i++)
        {
            if (vis[i] == true)
                pq.push(e[i]);
        }
        // 从大往小处理pq中的边
        while (!pq.empty())
        {
            Edge now = pq.top();
            pq.pop();
            int u = now.a;
            int v = now.b;
            int r = now.r;
            int p = now.p;
            int id = now.id;
            if (vis[id] == false)
                continue;
            ans[u] = min(ans[u], now.r);
            vis[id] = false;
            // cout << u << "," << ans[u] << "\n";
            outD[u]--;
            if (outD[u] == 0)
            {
                // u的答案确定了
                q.push(u);
                while (!q.empty())
                {
                    int u = q.front();
                    q.pop();
                    for (int i = 0; i < eee[u].size(); i++)
                    {
                        int v = eee[u][i].b;
                        int r = eee[u][i].r;
                        int p = eee[u][i].p;
                        int id = eee[u][i].id;
                        if (vis[id]==false)
                            continue;
                        vis[id] = false;
                        ans[v] = min(ans[v], max(r, ans[u] - p));
                        // cout << v << ":" << ans[v] << "\n";
                        outD[v]--;
                        if (outD[v] == 0)
                            q.push(v);
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++)
            cout << ans[i] << " ";
        return 0;
    }
    
    • 1

    信息

    ID
    8906
    时间
    1000ms
    内存
    128MiB
    难度
    6
    标签
    递交数
    0
    已通过
    0
    上传者