06-4. How Long Does It Take (25)拓扑排序 求关键路径的最长的长度
2015-01-05 21:56
411 查看
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 12 0 1 6 0 2 4 0 3 5 1 4 1 2 4 1 3 5 2 5 4 0 4 6 9 4 7 7 5 7 4 6 8 2 7 8 4
Sample Output 1:
18
Sample Input 2:
4 5 0 1 1 0 2 2 2 1 3 1 3 4 3 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数组的最大元素 else puts("Impossible"); return 0; }
相关文章推荐
- PAT 数据结构 06-图7. How Long Does It Take (25) 拓扑排序 最早开始时间
- 06-图7. How Long Does It Take (25)
- 06-图7. How Long Does It Take (25)
- PAT(MOOC-数据结构) 06-4. How long does it take (25)
- 06-图7. How Long Does It Take (25)
- 08-图8 How Long Does It Take(25 point(s)) 拓扑排序
- 中国大学MOOC-数据结构基础习题集、06-4、How Long Does It Take
- PAT--How Long Does It Take (25)
- 7-11 How Long Does It Take(25 分)(toposort)
- 08-图8 How Long Does It Take(25 分)
- mooc -- 06-4 How Long Does It Take
- 拓扑排序-图8 How Long Does It Take
- 关键路径算法(PAT How Long Does It Take (25))
- 08-图8 How Long Does It Take
- 08-图8 How Long Does It Take
- MOOC PTA 08-图8 How Long Does It Take
- PAT How Long Does It Take (25) (拓扑排序)
- 陈越何欣铭老师数据结构PTA08-图8 How Long Does It Take
- How long does it take to make a context switch?
- 08-图8 How Long Does It Take