• 08-图8 How Long Does It Take(浙大数据结构)


    中国大学MOOC-陈越、何钦铭-数据结构-2022夏

    2 天

    08-图8 How Long Does It Take

    分数 25

    作者 陈越

    单位 浙江大学

    Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N−1), and M, the number of activities. Then M lines follow, each gives the description of an activity. For the i-th activity, three non-negative numbers are given: S[i], E[i], and L[i], where S[i] is the index of the starting check point, E[i] of the ending check point, and L[i] the lasting time of the activity. The numbers in a line are separated by a space.

    Output Specification:

    For each test case, if the scheduling is possible, print in a line its earliest completion time; or simply output "Impossible".

    Sample Input 1:

    1. 9 12
    2. 0 1 6
    3. 0 2 4
    4. 0 3 5
    5. 1 4 1
    6. 2 4 1
    7. 3 5 2
    8. 5 4 0
    9. 4 6 9
    10. 4 7 7
    11. 5 7 4
    12. 6 8 2
    13. 7 8 4

    Sample Output 1:

    18
    

    Sample Input 2:

    1. 4 5
    2. 0 1 1
    3. 0 2 2
    4. 2 1 3
    5. 1 3 4
    6. 3 2 5

    Sample Output 2:

    Impossible

    coding :

    1. #include <iostream>
    2. #include <algorithm>
    3. #include <queue>
    4. #include <stack>
    5. using namespace std;
    6. struct ENode
    7. {
    8. int v;
    9. int cost;
    10. };
    11. const int maxn = 110;
    12. vector<ENode> Adj[maxn];
    13. int ve[maxn];
    14. int inDegree[maxn];
    15. stack<int> topOrder;
    16. int Nv,Ne;
    17. void Read(); //读入数据
    18. bool topSort();
    19. int main()
    20. {
    21. Read();
    22. if(topSort()==false)
    23. cout << "Impossible\n";
    24. else
    25. {
    26. //cout << ve[Nv-1] << endl; //直接输出ve[Nv-1] 有可能不对,当最后完成的任务不是第Nv件事件时;
    27. cout << *max_element(ve,ve+Nv) << endl;
    28. }
    29. return 0;
    30. }
    31. void Read() //读入数据
    32. {
    33. cin >> Nv >> Ne;
    34. for(int i=0;i<Ne;++i)
    35. {
    36. int u,v,cost;
    37. cin >> u >> v >> cost;
    38. Adj[u].push_back({v,cost});
    39. inDegree[v]++;
    40. }
    41. }
    42. bool topSort()
    43. {
    44. //fill(ve,ve+Nv,0);
    45. queue<int> q;
    46. for(int i=0;i<Nv;++i)
    47. if(inDegree[i]==0)
    48. q.push(i);
    49. while(!q.empty())
    50. {
    51. int u=q.front();
    52. q.pop();
    53. topOrder.push(u);
    54. for(int i=0;i<Adj[u].size();++i)
    55. {
    56. int v=Adj[u][i].v;
    57. inDegree[v]--;
    58. if(inDegree[v]==0)
    59. q.push(v);
    60. if(ve[u]+Adj[u][i].cost > ve[v])
    61. {
    62. ve[v]=ve[u]+Adj[u][i].cost;
    63. }
    64. }
    65. }
    66. if(topOrder.size() == Nv)
    67. return true;
    68. else
    69. return false;
    70. }

  • 相关阅读:
    ‘Tensor‘ object has no attribute ‘np‘
    “不要用 Edge 默认的必应,我被骗了”
    数据库知识:SQLServer创建非sa用户笔记
    单调栈问题---(每日温度,下一个更大元素Ⅰ)
    基于北方苍鹰优化算法的函数寻优算法
    查看tensorflow是否支持GPU,以及测试程序
    远程桌面访问MATLAB 2018B,提示License Manger Error -103,终极解决方案
    java基础:java安装、配置环境变量
    电脑开机屏幕闪烁,怎么解决
    【Java 基础篇】Java 自动装箱与拆箱:优雅处理基本数据类型与包装类的转换
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/126675245