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

原题

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;
}
相关推荐
We་ct10 分钟前
LeetCode 73. 矩阵置零:原地算法实现与优化解析
前端·算法·leetcode·矩阵·typescript
天赐学c语言10 分钟前
2.1 - 反转字符串中的单词 && 每个进程的内存里包含什么
c++·算法·leecode
程序员泠零澪回家种桔子12 分钟前
OpenManus开源自主规划智能体解析
人工智能·后端·算法
请注意这个女生叫小美14 分钟前
C语言 实例20 25
c语言·开发语言·算法
好学且牛逼的马15 分钟前
【Hot100|22-LeetCode 206. 反转链表 - 完整解法详解】
算法·leetcode·矩阵
hans汉斯18 分钟前
国产生成式人工智能解决物理问题能力研究——以“智谱AI”、“讯飞星火认知大模型”、“天工”、“360智脑”、“文心一言”为例
大数据·人工智能·算法·aigc·文心一言·汉斯出版社·天工
v_for_van21 分钟前
力扣刷题记录3(无算法背景,纯C语言)
c语言·算法·leetcode
ValhallaCoder25 分钟前
hot100-矩阵
数据结构·python·算法·矩阵
散峰而望26 分钟前
【基础算法】穷举的艺术:在可能性森林中寻找答案
开发语言·数据结构·c++·算法·随机森林·github·动态规划
心.c28 分钟前
Vue3+Node.js实现文件上传分片上传和断点续传【详细教程】
前端·javascript·vue.js·算法·node.js·哈希算法