当前位置: 首页 > article >正文

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

原题

#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;
}


http://www.kler.cn/a/589034.html

相关文章:

  • Python的那些事第四十五篇:继承自Nose的测试框架Nose2
  • 区间预测 | Matlab实现QRBiTCN分位数回归双向时间卷积神经网络注意力机制时序区间预测
  • Ubuntu 一站式初始化笔记
  • 【sql靶场】第13、14、17关-post提交报错注入保姆级教程
  • JVM常用概念之超态虚拟调用
  • 解析GNGGA数据,C语言单片机
  • AI是如何实现屏幕触控防水? 实测华为畅享70X
  • Redis监控:从睁眼瞎到千里眼的进化史
  • 【go语言圣经1.6】
  • 19.如何使用 pandas 处理大型 Excel 文件:并行读取工作表
  • pytorch小记(八):pytorch中有关于.detach()的浅显见解
  • PostgreSQL技术内幕26:PG聚合算子实现分析
  • 【VBA】excel获取股票实时行情(历史数据,基金数据下载)
  • 量子计算与医疗诊断的未来:超越传统的无限可能
  • 深度学习篇---Opencv中Haar级联分类器的自定义
  • Java 大视界 -- 基于 Java 的大数据机器学习模型的迁移学习应用与实践(129)
  • 五大基础算法——贪心算法
  • 各省水资源平台 水资源遥测终端机都用什么协议
  • CSS中绝对定位
  • 【报错问题】在visual studio 终端使用npm -v后报错禁止运行脚本怎么处理