(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

相关文章

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav