Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. const int MAX = 500 + 1;
  7.  
  8. int N;
  9. int inDegree[MAX];
  10. int duration[MAX];
  11. int result[MAX];
  12. vector<int> graph[MAX];
  13.  
  14. int main(void)
  15. {
  16. ios_base::sync_with_stdio(0);
  17. cin.tie(0);
  18. cin >> N;
  19.  
  20. for (int i = 1; i <= N; i++)
  21. {
  22. int time;
  23. cin >> time;
  24.  
  25. duration[i] = time;
  26.  
  27. while (1)
  28. {
  29. int node;
  30. cin >> node;
  31.  
  32. if (node == -1)
  33. {
  34. break;
  35. }
  36.  
  37. inDegree[i]++;
  38. graph[node].push_back(i);
  39. }
  40. }
  41.  
  42. queue<int> q;
  43.  
  44. for (int i = 1; i <= N; i++)
  45. {
  46. if (inDegree[i] == 0)
  47. {
  48. q.push(i);
  49. }
  50. }
  51.  
  52.  
  53.  
  54. while (!q.empty())
  55. {
  56. int node = q.front();
  57. q.pop();
  58.  
  59. for (int i = 0; i < graph[node].size(); i++)
  60. {
  61. int nextNode = graph[node][i];
  62.  
  63. result[nextNode] = max(result[nextNode], result[node] + duration[node]);
  64.  
  65. if (--inDegree[nextNode] == 0)
  66. {
  67. q.push(nextNode);
  68. }
  69. }
  70. }
  71.  
  72. for (int i = 1; i <= N; i++)
  73. {
  74. cout << result[i] + duration[i] << "\n";
  75. }
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement