您的位置:首页 > 其它

POJ 1949 Chores (DP+拓扑)

2010-04-27 18:26 323 查看
题意:有n个任务,每个任务k可能与之前1-k个任务有关,就是说只有当其所有的先前任务完成时,这个任务才可以开始,有给定了每个任务的执行时间,求要完成所有的任务,最少需要多少时间。

思路:由于任务前后有联系,所以自然想到了有向图,DP,拓扑,仔细想想发现,一个任务至于前k个任务有关,所以我们就用不到拓扑排序了,只需要记录当前任务最快完成的时间即可,

    end_time[k] = max{end_time[i]}, 0<i<k

所以我们可以边输入边记录,随时更新最大值,最后输出最大值就可以了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <bitset>
#include <queue>
#include <vector>
using namespace std;

const int BORDER = (1<<20)-1;
const int MAXSIZE = 37;
const int MAXN = 10005;
const int INF = 1000000000;
#define CLR(x,y) memset(x,y,sizeof(x))
#define ADD(x) x=((x+1)&BORDER)
#define IN(x) scanf("%d",&x)
#define OUT(x) printf("%d\n",x)
#define MIN(m,v) (m)<(v)?(m):(v)
#define MAX(m,v) (m)>(v)?(m):(v)
#define ABS(x) ((x)>0?(x):-(x))

int ed_tim[MAXN],n,ans;

int init()
{
ans = 0;
CLR(ed_tim,0);
return 0;
}
int work()
{
int i,j,tmp,m,mmax,cur_tim;
for(i = 1; i <= n; ++i)
{
mmax = 0;
scanf("%d%d",&cur_tim,&m);
for(j = 0; j < m; ++j)
{
IN(tmp);
mmax = MAX(mmax,ed_tim[tmp]);
}
ed_tim[i] = mmax + cur_tim;
ans = MAX(ans,ed_tim[i]);
}
OUT(ans);
return 0;
}
int main()
{
while(IN(n)!=EOF)
{
init();
work();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: