POJ 1661 记忆化搜索

68 篇文章 0 订阅

测试数据:


7
3 8 17 20
0 10 8
0 10 13
4 14 3


3 8 7 2
6 14 6
4 10 4
5 14 2


1 6 10 20
2 3 5


10 899 52 50
893 903 18
890 900 38
898 908 8
910 920 8
894 904 43
881 891 18
872 882 38
867 877 43
842 852 43
895 905 3


3 8 17 20
8 10 8
8 10 13
8 14 3


60 822 302 50
813 823 298
813 823 293
816 826 213
816 826 178
817 827 218
813 823 148
821 831 73
814 824 248
813 823 283
815 825 158
819 829 58
814 824 13
813 823 28
819 829 233
814 824 43
773 783 293
821 831 93
818 828 268
816 826 198
818 828 113
814 824 208
816 826 68
821 831 133
794 804 248
814 824 108
829 839 28
818 828 143
844 854 298
802 812 133
801 811 28
818 828 238
817 827 8
816 826 48
820 830 3
819 829 288
822 832 138
820 830 183
855 865 13
777 787 268
820 830 63
789 799 28
822 832 33
855 865 213
779 789 208
836 846 248
806 816 33
821 831 263
818 828 83
846 856 263
789 799 68
854 864 8
854 864 283
801 811 298
805 815 143
822 832 23
821 831 173
813 823 153
858 868 138
818 828 98
839 849 133


4 14 5 6
3 22 1
16 23 2
16 30 3
13 21 4

ans:

23
17
10
61
17
352

15


#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;

int inf=2000000000;
int n,Max;
struct comp
{
    int l,r,y;
}data[1010];

struct node
{
    int l,r;
}dp[1010];

int min(int a,int b)
{
    if (a<b) return a; else return b;
}

bool cmp(const comp a,const comp b)
{
    return a.y>b.y;
}
void dfs(int w)
{
    int i,now;
    if (data[w].y==0) return ;

    if (dp[w].l!=inf || dp[w].r!=inf ) return ;

 //查找从左边掉落的最优值
    for (i=w+1;i<=n+1;i++)
        if (data[i].l<=data[w].l &&data[i].r>=data[w].l)
        {
            now=i;
            break;
        }
    if (data[w].y-data[now].y>Max) dp[w].l=inf;
    else
    {
    dfs(now);
    if (data[now].y==0) dp[w].l=0;
    else
    dp[w].l=min(dp[now].l+data[w].l-data[now].l,dp[now].r+data[now].r-data[w].l);
    }

//查找从右边掉落的最优值
    for (i=w+1;i<=n+1;i++)
        if (data[i].l<=data[w].r &&data[i].r>=data[w].r)
        {
            now=i;
            break;
        }
    if (data[w].y-data[now].y>Max) dp[w].r=inf;
    else
    {
    dfs(now);
    if (data[now].y==0) dp[w].r=0;
    else
    dp[w].r=min(dp[now].l+data[w].r-data[now].l,dp[now].r+data[now].r-data[w].r);
    }

    return ;

}
int main()
{
    int t,x,y,i;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d%d%d",&n,&x,&y,&Max);
        for (i=1;i<=n;i++)
            scanf("%d%d%d",&data[i].l,&data[i].r,&data[i].y);

        data[0].l=data[0].r=x; data[0].y=y;
        data[n+1].l=-20000; data[n+1].r=20000; data[n+1].y=0;
        sort(data,data+n+2,cmp);

        for (i=0;i<=n;i++)
            dp[i].l=dp[i].r=inf;
        dp[n+1].l=dp[n+1].r=0;

        dfs(0);
        printf("%d\n",dp[0].l+y);


    }
    return 0;
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值