(POJ 1949)Chores DAG简单DP

2024-02-05 02:18
文章标签 简单 dp poj 1949 dag chores

本文主要是介绍(POJ 1949)Chores DAG简单DP,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Chores
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 6129 Accepted: 2881
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.

]
Source

USACO 2002 February

题意:
有n个工作要完成,完成每个工作要花一定的时间,而每一个工作可能会有一些前提工作,前提工作没完成就不能做这项工作。多个工作可以同时进行,问你完成所有工作的最少时间为多少?

分析:
读题之后就知道题目是要求关键路径的长度,即最长路的长度。
此时,我们只需要处理好边的权值即可,设w[i]表示完场工作i的时间,所以u->v的权值为w[u],最后d[i]+=w[i],求最大值即可。
所以我就直接写了spfa()算法,然而超时了。。。。。
由于n<=1e4 所以 m <= n * n <= 1e8,所以会超时。。。。。

由此我们知道,工作v完成的最早时间是他的前驱工作完成的最早时间的最大值+w[v],所以我们就可以直接dp了。
并且,题目的输入是有序的,且保证了前驱工作的编号一定小于当前工作编号,所以可以直接“模拟”一遍即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;const int maxn = 10010;
int dp[maxn],ans;int main()
{int n,u,p,w;while(scanf("%d",&n)!=EOF){ans = 0;for(int i=1;i<=n;i++){scanf("%d%d",&w,&p);int tmp = 0;for(int j=0;j<p;j++){scanf("%d",&u);tmp = max(tmp,dp[u]);}dp[i] = tmp + w;ans = max(ans,dp[i]);}printf("%d\n",ans);}return 0;
}

最长路TLE代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;const int maxn = 10010;
struct edge
{int v,w,next;
}edges[maxn*maxn/2];int n,e;
int head[maxn],d[maxn],w[maxn];
bool vis[maxn];void addedges(int u,int v,int w)
{edges[e].v = v;edges[e].w = w;edges[e].next = head[u];head[u] = e++;
}void spfa()
{queue<int> q;memset(vis,0,sizeof(vis));memset(d,0,sizeof(d));q.push(1);vis[1] = 1;while(!q.empty()){int u = q.front(); q.pop();vis[u] = 0;for(int i=head[u];i!=-1;i=edges[i].next){int v = edges[i].v;int w = edges[i].w;if(d[v] < d[u] + w){d[v] = d[u] + w;if(vis[v] == 0){q.push(v);vis[v] = 1;}}}}
}int main()
{int p,u;while(scanf("%d",&n)!=EOF){e = 0;memset(head,-1,sizeof(head));for(int i=1;i<=n;i++){scanf("%d%d",&w[i],&p);for(int j=0;j<p;j++){scanf("%d",&u);addedges(u,i,w[u]);}}spfa();int ans = 0;for(int i=1;i<=n;i++){ans = max(d[i]+w[i],ans);}printf("%d\n",ans);}return 0;
}

这篇关于(POJ 1949)Chores DAG简单DP的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679493

相关文章

GO语言实现串口简单通讯

《GO语言实现串口简单通讯》本文分享了使用Go语言进行串口通讯的实践过程,详细介绍了串口配置、数据发送与接收的代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录背景串口通讯代码代码块分解解析完整代码运行结果背景最近再学习 go 语言,在某宝用5块钱买了个

SpringBoot整合Apache Spark实现一个简单的数据分析功能

《SpringBoot整合ApacheSpark实现一个简单的数据分析功能》ApacheSpark是一个开源的大数据处理框架,它提供了丰富的功能和API,用于分布式数据处理、数据分析和机器学习等任务... 目录第一步、添加android依赖第二步、编写配置类第三步、编写控制类启动项目并测试总结ApacheS

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

Java中使用 @Builder 注解的简单示例

《Java中使用@Builder注解的简单示例》@Builder简化构建但存在复杂性,需配合其他注解,导致可变性、抽象类型处理难题,链式编程非最佳实践,适合长期对象,避免与@Data混用,改用@G... 目录一、案例二、不足之处大多数同学使用 @Builder 无非就是为了链式编程,然而 @Builder

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构