失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 06-4. How Long Does It Take (25)拓扑排序 求关键路径的最长的长度

06-4. How Long Does It Take (25)拓扑排序 求关键路径的最长的长度

时间:2019-06-27 03:28:50

相关推荐

06-4. How Long Does It Take (25)拓扑排序  求关键路径的最长的长度

06-4. How Long Does It Take (25)

时间限制 200 ms

内存限制 65536 kB

代码长度限制 8000 B

判题程序 Standard 作者 CHEN, Yue

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:

9 120 1 60 2 40 3 51 4 12 4 13 5 25 4 04 6 94 7 75 7 46 8 27 8 4

Sample Output 1:

18

Sample Input 2:

4 50 1 10 2 22 1 31 3 43 2 5

Sample Output 2:

Impossible

#include <iostream>#include <algorithm>#include <string>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include<queue>#include<stack>#include<map>#include<set>using namespace std;#define lson rt<<1,l,MID#define rson rt<<1|1,MID+1,r//#define lson root<<1//#define rson root<<1|1#define MID ((l+r)>>1)typedef long long ll;typedef pair<int,int> P;const int maxn=3005;const int base=1000;const int inf=9999999999;const double eps=1e-5;int n,m;struct node{int to,cost;};vector<node> G[maxn];//邻接表int d[maxn];int earlist[maxn];//活动最早结束的数组bool tupo_sort()//拓扑排序 求最长的路径{stack<int> s;for(int i=0;i<n;i++){if(d[i]==0)s.push(i);}int cnt=0;//用于判断是否有环while(!s.empty()){int m=s.top();cnt++;s.pop();for(int i=0;i<G[m].size();i++){node tmp=G[m][i];if(--d[tmp.to]==0)s.push(tmp.to);if(earlist[m]+tmp.cost>earlist[tmp.to])//计算最长的路径{earlist[tmp.to]=earlist[m]+tmp.cost;}}}if(cnt<n)return false;//判断是否有环return true;}int main(){int i,j,k,t;cin>>n>>m;for(i=0;i<m;i++){node e;int s;cin>>s>>e.to>>e.cost;G[s].push_back(e);d[e.to]++;}if(tupo_sort())printf("%d\n",*max_element(earlist,earlist+n));//输出earlist数组的最大元素 elseputs("Impossible");return 0;}

如果觉得《06-4. How Long Does It Take (25)拓扑排序 求关键路径的最长的长度》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。