PTA 5-12 How Long Does It Take (25分)
Posted 我是一个小木匠!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; }
以上是关于PTA 5-12 How Long Does It Take (25分)的主要内容,如果未能解决你的问题,请参考以下文章
08-图8 How Long Does It Take (25 分)
08-图8 How Long Does It Take (25分)