2016, IX Samara Regional Intercollegiate Programming Contest I. Deadline

2016, IX Samara Regional Intercollegiate Programming Contest I. Deadline
题目看这里

Alex works as a developer, and he has hard times now. He has to complete n tasks. Every task i has the latest time di when it can be completed (starting from the moment of time 0), the time ci needed to complete it, and the tasks that must be completed before task i to get the possibility to start it. We’ll say that the i-th task depends on these tasks.

What should be the order of doing tasks to complete all of them in time?

Input
The first line contains a single integer n (1 ≤ n ≤ 200000) — the number of tasks.

Next n lines describe tasks. The i-th line starts with three space-separated integers di, ci and ri (1 ≤ di,  ci ≤ 109,  0 ≤ ri ≤ n - 1) — the latest time to complete the i-th task, the time needed to complete it and the number of tasks which it depends on. Then in the same line ri space-separated integers — the numbers of these tasks — are written. It’s guaranteed that there is no number i among these ri numbers.

The tasks are numbered from 1 in order of their appearance in the input. The sum of all ri in the input doesn’t exceed 200000.

Output
In the first line output «YES» (without quotes) if Alex will be able to complete all the tasks without exceeding any deadlines, or «NO» (without quotes) otherwise.

If the answer is «YES», in the second line output n space-separated integers — the numbers of tasks in the order they must be done to fit into all deadlines. If there are many solutions, output any of them.

算法:贪心,dfs,优先队列

题意:共有n个任务,每个任务有对应的deadline d和花费的时间 c,在做第i个任务前必须保证它的前置任务ri已经做完,前置任务可以没有。
判断是否能在ddl之前完成任务,如能输出任意一种顺序。

核心思路
先贪心将任务按照deadline从小到大排序,然后依次做任务,如果当前任务有前置任务,将前置任务排序按照ddl从小到大排序后将前置任务依次做。
这里涉及到一个递归,可能并不是最优解,使用拓扑排序效率会更高。

这里使用优先队列存储任务,dfs搜索,state[i]判断是否有环,vis[i]判断该任务是否已做,每次把now+a[i]的所花费的时间加上和a[i]的ddl比较,如果大于则no,否则将a[i].num存入vector中。

#include<bits/stdc++.h>
using namespace std;
const int maxn=200005int n,i,j,k,r,w,now=0;      //now:currentTime
struct node{
   
	int d,c,num
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值