首页 > 代码库 > PTA 5-12 How Long Does It Take (25分)
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分)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。