题目:https://www.luogu.com.cn/problem/P1113
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52//拓扑排序 #include<iostream> #include<queue> #include<algorithm> #include<vector> #include<stack> using namespace std; int n; vector<int> v[10005];//记录出度编号 int ru[10005];//记录入度的个数 int ti[10005];//时间 int an[10005];//记录结束时间 int ans=0; int maxt=0; queue<int> s; int main(){ cin>>n; for(int i=1;i<=n;i++){ int j,t; cin>>j>>ti[i]; int x; while(cin>>x&&x!=0){ v[x].push_back(i); ru[i]++; } } //考虑到可能有多个入度为0的点 for(int i=1;i<=n;i++){ if(ru[i] == 0){ s.push(i); an[i]=ti[i]; } } while(!s.empty()){ int p = s.front(); s.pop(); for(int i=0;i<v[p].size();i++){ int q=v[p][i]; ru[q]--; if(ru[q] == 0){ s.push(q); } an[q] = max(an[q],an[p]+ti[q]); } } for(int i=1;i<=n;i++){ ans=max(ans,an[i]); } cout<<ans<<endl; return 0; }
最后
以上就是激动小蝴蝶最近收集整理的关于拓扑排序模板题洛谷 p1113的全部内容,更多相关拓扑排序模板题洛谷内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复