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

原题

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;
}
相关推荐
燃于AC之乐19 分钟前
我的算法修炼之路--4 ———我和算法的爱恨情仇
算法·前缀和·贪心算法·背包问题·洛谷
MM_MS6 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
独自破碎E6 小时前
【二分法】寻找峰值
算法
mit6.8247 小时前
位运算|拆分贪心
算法
ghie90907 小时前
基于MATLAB的TLBO算法优化实现与改进
开发语言·算法·matlab
恋爱绝缘体17 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
wuk9987 小时前
VSC优化算法MATLAB实现
开发语言·算法·matlab
Z1Jxxx7 小时前
加密算法加密算法
开发语言·c++·算法
乌萨奇也要立志学C++8 小时前
【洛谷】递归初阶 三道经典递归算法题(汉诺塔 / 占卜 DIY/FBI 树)详解
数据结构·c++·算法
vyuvyucd8 小时前
C++引用:高效编程的别名利器
算法