fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. freopen("input.txt", "r", stdin);
  6.  
  7. // Variables for input and graph processing
  8. int n, m, node, target, cost;
  9. char separator;
  10.  
  11. // Data structures
  12. vector<int> processingTime; // Stores processing time for each node
  13. vector<int> indegree; // Keeps track of the number of prerequisites for each node
  14. vector<vector<array<int, 2>>> adjacencyList; // Graph adjacency list with [target, cost]
  15. vector<int> longestPathEnd; // Tracks the node with the longest path to the current node
  16. vector<int> distance; // Stores the longest path distance to each node
  17. vector<int> stack; // Stack for processing nodes
  18.  
  19. while (cin >> n >> m) {
  20. // Resize and initialize data structures
  21. processingTime.resize(n, 0);
  22. indegree.resize(n, 0);
  23. adjacencyList.resize(n);
  24. longestPathEnd.resize(n, -1);
  25. distance.resize(n, 0);
  26.  
  27. // Input processing times
  28. cin >> processingTime[0];
  29. for (int i = 1; i < n; ++i) {
  30. cin >> separator >> processingTime[i];
  31. }
  32.  
  33. // Input edges and build the graph
  34. for (int i = 0; i < m; ++i) {
  35. cin >> node >> target >> cost;
  36. indegree[target]++;
  37. adjacencyList[node].push_back({target, cost});
  38. }
  39.  
  40. // Process nodes to calculate the longest path
  41. for (int i = n - 1; i >= 0; --i) {
  42. stack.push_back(i); // Add current node to the stack
  43. while (!stack.empty()) {
  44. int currentNode = stack.back();
  45. stack.pop_back();
  46.  
  47. // If prerequisites are satisfied
  48. if (--indegree[currentNode] == -1) {
  49. distance[currentNode] += processingTime[currentNode];
  50.  
  51. // Process all adjacent nodes
  52. for (auto &neighbor : adjacencyList[currentNode]) {
  53. int nextNode = neighbor[0];
  54. int edgeWeight = neighbor[1];
  55.  
  56. // Update distance and path information
  57. if (distance[nextNode] < distance[currentNode] + edgeWeight) {
  58. longestPathEnd[nextNode] = (longestPathEnd[currentNode] == -2) ? -2 : currentNode;
  59. distance[nextNode] = distance[currentNode] + edgeWeight;
  60. } else if (distance[nextNode] == distance[currentNode] + edgeWeight) {
  61. longestPathEnd[nextNode] = -2; // Mark ambiguous paths
  62. }
  63.  
  64. stack.push_back(nextNode); // Add to stack for processing
  65. }
  66. }
  67. }
  68. }
  69.  
  70. // Find the node with the maximum distance
  71. int endNode = max_element(distance.begin(), distance.end()) - distance.begin();
  72. cout << distance[endNode];
  73.  
  74. // Check if the longest path is unique
  75. if (longestPathEnd[endNode] != -2 && count(distance.begin(), distance.end(), distance[endNode]) == 1) {
  76. // Reconstruct the path
  77. stack.push_back(endNode);
  78. while (stack.back() != -1) {
  79. stack.push_back(longestPathEnd[stack.back()]);
  80. }
  81. stack.pop_back(); // Remove the invalid -1 marker
  82.  
  83. // Output the path
  84. while (!stack.empty()) {
  85. cout << "," << stack.back();
  86. stack.pop_back();
  87. }
  88. cout << "\n";
  89. } else {
  90. cout << ",M\n"; // Ambiguous path
  91. }
  92. }
  93.  
  94. return 0;
  95. }
  96.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty