POJ1949 Chores 解题报告【拓扑排序/DAG最长路】
来源:互联网 发布:视频软件管理条例2017 编辑:程序博客网 时间:2024/05/17 23:11
Description
Farmer John’s family pitches in with the chores during milking, doing all the chores as quickly as possible. At FJ’s house, some chores cannot be started until others have been completed, e.g., it is impossible to wash the cows until they are in the stalls.
Farmer John has a list of N (3 <= N <= 10,000) chores that must be completed. Each chore requires an integer time (1 <= length of time <= 100) to complete and there may be other chores that must be completed before this chore is started. We will call these prerequisite chores. At least one chore has no prerequisite: the very first one, number 1. Farmer John’s list of chores is nicely ordered, and chore K (K > 1) can have only chores 1,.K-1 as prerequisites. Write a program that reads a list of chores from 1 to N with associated times and all perquisite chores. Now calculate the shortest time it will take to complete all N chores. Of course, chores that do not depend on each other can be performed simultaneously.
Input
* Line 1: One integer, N
* Lines 2..N+1: N lines, each with several space-separated integers. Line 2 contains chore 1; line 3 contains chore 2, and so on. Each line contains the length of time to complete the chore, the number of the prerequisites, Pi, (0 <= Pi <= 100), and the Pi prerequisites (range 1..N, of course).
Output
A single line with an integer which is the least amount of time required to perform all the chores.
Sample Input
7
5 0
1 1 1
3 1 2
6 1 1
1 2 2 4
8 2 2 4
4 3 3 5 6
Sample Output
23
Hint
[
Here is one task schedule:
Chore 1 starts at time 0, ends at time 5.
Chore 2 starts at time 5, ends at time 6.
Chore 3 starts at time 6, ends at time 9.
Chore 4 starts at time 5, ends at time 11.
Chore 5 starts at time 11, ends at time 12.
Chore 6 starts at time 11, ends at time 19.
Chore 7 starts at time 19, ends at time 23.
]
解题报告
题意就是说要完成n个任务,这n个任务都有自己的先决条件,由此读入的时候需要反向建边。此外,由于这些先决条件可以同时进行,那么他们完成的时间也就是这几个之中的最大值(也就是点权的最大值)。这样一来就很明朗了。
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int N=10000,M=1000000;struct edge{ int v,next;}ed[M+5];int head[N+5],num;int n,w[N+5],in[N+5],out[N+5],dis[N+5],vmax;void build(int u,int v){ ed[++num].v=v; ed[num].next=head[u]; head[u]=num;}void toposort(){ queue<int>q; for(int i=1;i<=n;i++)dis[i]=-0x7fffffff; for(int u=1;u<=n;u++) if(!in[u]){q.push(u);dis[u]=w[u];} while(!q.empty()) { int u=q.front();q.pop(); for(int i=head[u];i!=-1;i=ed[i].next) { int v=ed[i].v; dis[v]=max(dis[v],dis[u]+w[v]); if(!(--in[v]))q.push(v); } }}int main(){ memset(head,-1,sizeof(head)); scanf("%d",&n); for(int v=1;v<=n;v++) { int tot; scanf("%d%d",&w[v],&tot); while(tot--) { int u; scanf("%d",&u); build(u,v); in[v]++,out[u]++; } } toposort(); for(int v=1;v<=n;v++)if(!out[v])vmax=max(vmax,dis[v]); printf("%d",vmax); return 0;}
- POJ1949 Chores 解题报告【拓扑排序/DAG最长路】
- poj1949 DAG 最长路
- 【POJ】1949 Chores DAG最长路
- poj1949 chores
- POJ1949 (DAG图的最长路径)
- (求DAG图最长路)拓扑排序模板
- BZOJ 1093 [ZJOI 2007] Tarjan+DAG拓扑排序DP 解题报告
- poj1949 Chores SPFA
- 【BZOJ1194】[HNOI2006]潘多拉的盒子【BFS】【SCC】【拓扑排序】【DAG最长路】【自动机】
- HDU 5098 Smart Software Installer 双队列拓扑排序或者DAG的最长路
- 图论-------拓扑排序解题报告--poj 3687
- 解题报告-HDOJ-1285(拓扑排序)
- 杭电1285拓扑排序解题报告
- [解题报告]hdoj1285(拓扑排序)
- POJ 2367 拓扑排序 解题报告
- 洛谷P1983 拓扑排序 解题报告
- Poj Window Pains (拓扑排序) 解题报告
- DAG图与拓扑排序
- 指针
- 完全掌握Android Data Binding
- 多项式相乘的另一解法
- Android中Canvas绘图方法的实现
- 堆栈溢出 Maximum call stack size exceeded
- POJ1949 Chores 解题报告【拓扑排序/DAG最长路】
- textarea使用注意
- ubuntu 下杀死进程的方式
- Head First HTML与CSS学习(10.09)
- CSS基础
- hdu 2066 一个人的旅行(Dijkstra)
- 选择恐惧症的福音!教你认清MVC,MVP和MVVM
- 线程安全的单例模式
- 实例中的__class_属性链接到了它的类,而类的__bases__属性,是一个元组,包含了通往更高的超类的链接