CodeForces864E【DP+路径输出】

知道题意以后就是一个简单的:
0/1背包+路径输出???
MMP???写C的时间不如写E

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;

struct asd{
    int t, d, p, id;
}node[110];
bool cmp(asd x, asd y){
    return x.d < y.d;
}
int n;
int prex[110][2100], prey[110][2100];
pair<int,int>dp[2100];
vector<int>ans;

int main(){
    scanf("%d", &n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&node[i].t, &node[i].d, &node[i].p);
        node[i].id = i;
    }
    sort(node+1, node+n+1, cmp);
    for(int i=0;i<=2000;i++){
        dp[i].first = 0;
        dp[i].second = -1;
    }
    memset(prex, -1, sizeof(prex));

    for(int i=1;i<=n;i++){
        for(int j=node[i].d-node[i].t-1;j >= 0;j--){
            if(dp[j+node[i].t].first < dp[j].first + node[i].p)
            {
                prex[node[i].id][j+node[i].t] = dp[j].second;
                prey[node[i].id][j+node[i].t] = j;

                dp[j+node[i].t].first = dp[j].first + node[i].p;
                dp[j+node[i].t].second = node[i].id;
            }
        }
    }
    int Max = 0, pos;
    for(int i=0;i<=2000;i++)
        if(dp[i].first > Max){
            Max = dp[i].first;
            pos = i;
        }
    printf("%d\n", Max);
    int x, y, xx, yy;
    x = dp[pos].second;
    y = pos;
    while(x != -1){
        ans.push_back(x);
        xx = x;yy = y;
        x = prex[xx][yy];
        y = prey[xx][yy];
    }
    printf("%d\n", (int)ans.size());
    for(int i=(int)ans.size()-1;i>=0;i--)
        printf("%d ", ans[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值