• PTA 5-12 How Long Does It Take (25分)


    这题看不太懂题目啊~  参考的http://blog.csdn.net/qq_26437925/article/details/49420089?locationNum=6&fps=1

    先放着吧。

    #include  "iostream"
    #include "vector"
    using namespace std;
    int in_degree[101]; /* 记录图的各个节点的入度 */
    struct Node {
        int e;
        int cost;
    };
    vector<Node>v[101];
    int early[101];
    int n, m;
    bool toplogicSort() {
        int start = -1;
        for (int i = 0; i < n; i++) {
            if (in_degree[i] == 0) {
                start = i;
                break;
            }
        }
        if (start == -1) { /* 没有入度为0的点 完不成项目*/
            return false;
        }
        early[start] = 0;
        int sum = 0;
        int num = 0;
        int newP = start;
        while (num != n - 1) {
            int len = v[newP].size(); /* 出度 */
            for (int i = 0; i < len; i++) {
                int e = v[newP][i].e;
                int cost = v[newP][i].cost;
                in_degree[e]--;
                if (early[newP] + cost > early[e]) {
                    early[e] = early[newP] + cost;
                }
            }
            in_degree[newP] = -1; /* 删除该节点 */
            newP = -1;
            for (int i = 0; i < n; i++) {
                if (in_degree[i] == 0) {
                    newP = i;
                    break;
                }
            }
            if (newP == -1)
                return false;
            if (early[newP] > sum)
                sum = early[newP];
            num++;
        }
        cout << sum << endl;
        return true;
    }
    int main() {
        Node node;
        cin >> n >> m;
        for(int i = 0; i < n; i++){
        in_degree[i] = 0;
        early[i] = 0;
        }
        while (m--) {
            int s, e, l;
            cin >> s >> e >> l;
            node.e = e;
            node.cost = l;
            v[s].push_back(node);
            in_degree[e]++;
        }
        if (!toplogicSort())
            cout << "Impossible" << endl;
    }
  • 相关阅读:
    tensorflow模型保存和使用08
    LSTM最基本的应用(07-3)
    手写数字识别-卷积神经网络cnn(06-2)
    python面向对象笔记
    attention机制
    dockerfile
    docker入门
    CentOS7安装Oracle11gR2
    anaconda常用的命令
    python xlrd处理表格常用方法
  • 原文地址:https://www.cnblogs.com/minesweeper/p/6128012.html
Copyright © 2020-2023  润新知