(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

相关文章

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

利用Python编写一个简单的聊天机器人

《利用Python编写一个简单的聊天机器人》这篇文章主要为大家详细介绍了如何利用Python编写一个简单的聊天机器人,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 使用 python 编写一个简单的聊天机器人可以从最基础的逻辑开始,然后逐步加入更复杂的功能。这里我们将先实现一个简单的

使用IntelliJ IDEA创建简单的Java Web项目完整步骤

《使用IntelliJIDEA创建简单的JavaWeb项目完整步骤》:本文主要介绍如何使用IntelliJIDEA创建一个简单的JavaWeb项目,实现登录、注册和查看用户列表功能,使用Se... 目录前置准备项目功能实现步骤1. 创建项目2. 配置 Tomcat3. 项目文件结构4. 创建数据库和表5.

使用PyQt5编写一个简单的取色器

《使用PyQt5编写一个简单的取色器》:本文主要介绍PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16进制颜色编码,一款跟随鼠标刷新图像的RGB和16... 目录取色器1取色器2PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16

四种简单方法 轻松进入电脑主板 BIOS 或 UEFI 固件设置

《四种简单方法轻松进入电脑主板BIOS或UEFI固件设置》设置BIOS/UEFI是计算机维护和管理中的一项重要任务,它允许用户配置计算机的启动选项、硬件设置和其他关键参数,该怎么进入呢?下面... 随着计算机技术的发展,大多数主流 PC 和笔记本已经从传统 BIOS 转向了 UEFI 固件。很多时候,我们也

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个