算法——图论——关键活动

原题

cpp 复制代码
#include <iostream>
#include <vector>
#include <queue>
#include <set>

using namespace std;

struct edge {
    int destination;
    int dist;

    edge(int destination_, int dist_) : destination(destination_), dist(dist_) {}
};

vector<edge> graph[100];
vector<edge> reGraph[100];
vector<int> inDo(100, 0);
vector<int> outDo(100, 0);
vector<int> lessTime(100, 0);
vector<int> moreTime(100, 0x3fffffff);

int main() {

    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        graph[u].emplace_back(v, w);
        reGraph[v].emplace_back(u, w);
        outDo[u]++;
        inDo[v]++;
    }
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        if (inDo[i] == 0) {
            q.push(i);
            lessTime[i] = 0;
        }
    }

    while (!q.empty()) {
        int cur = q.front();
        q.pop();

        for (edge neighbor: graph[cur]) {
            inDo[neighbor.destination]--;
            lessTime[neighbor.destination] = max(lessTime[neighbor.destination], lessTime[cur] + neighbor.dist);
            if (inDo[neighbor.destination] == 0) {
                q.push(neighbor.destination);
            }
        }
    }

    int totalTime = 0;
    for (int i = 0; i < n; ++i) {
        if (inDo[i] != 0) {
            cout << "No" << endl;
            return 0;
        }
        totalTime = max(totalTime, lessTime[i]);
    }

    for (int i = 0; i < n; ++i) {
        if (outDo[i] == 0) {
            q.push(i);
            moreTime[i] = totalTime;
        }
    }


    while (!q.empty()) {
        int cur = q.front();
        q.pop();

        for (edge neighbor: reGraph[cur]) {
            outDo[neighbor.destination]--;
            moreTime[neighbor.destination] = min(moreTime[neighbor.destination], moreTime[cur] - neighbor.dist);
            if (outDo[neighbor.destination] == 0) {
                q.push(neighbor.destination);
            }
        }
    }

    set<pair<int, int>> s;
    for (int i = 0; i < n; ++i) {
    for (edge neighbor: graph[i]) {
        if (moreTime[neighbor.destination] - lessTime[i] - neighbor.dist == 0) {
            s.insert({i, neighbor.destination});
        }
    }
}

//    for (int i = 0; i < n; ++i) {
//        if (lessTime[i] == 0 && moreTime[i] == 0) {
//            q.push(i);
//        }
//    }

//    while (!q.empty()) {
//        auto cur = q.front();
//        q.pop();

//        for (auto neighbor: graph[cur]) {
//            if (lessTime[neighbor.destination] == moreTime[neighbor.destination]) {
//                s.insert({cur, neighbor.destination});
//            q.push(neighbor.destination);
//            }
//        }
//    }

    cout << "Yes" << endl;
    for (auto p: s) {
        cout << p.first << " " << p.second << endl;
    }
    return 0;
}
相关推荐
伏小白白白21 小时前
【论文精度-2】求解车辆路径问题的神经组合优化算法:综合展望(Yubin Xiao,2025)
人工智能·算法·机器学习
无敌最俊朗@21 小时前
数组-力扣hot56-合并区间
数据结构·算法·leetcode
囚生CY1 天前
【速写】优化的深度与广度(Adam & Moun)
人工智能·python·算法
码农多耕地呗1 天前
力扣94.二叉树的中序遍历(递归and迭代法)(java)
数据结构·算法·leetcode
懒羊羊不懒@1 天前
Java基础语法—最小单位、及注释
java·c语言·开发语言·数据结构·学习·算法
白云千载尽1 天前
leetcode 912.排序数组
算法·leetcode·职场和发展
哆啦刘小洋1 天前
Tips:预封装约束的状态定义
算法
代码充电宝1 天前
LeetCode 算法题【简单】290. 单词规律
java·算法·leetcode·职场和发展·哈希表
Juan_20121 天前
P1040题解
c++·算法·动态规划·题解
Onesoft%J1ao1 天前
C++竞赛递推算法-斐波那契数列常见题型与例题详解
c++·算法·动态规划·递推·信息学奥赛