POJ 1661 记忆化搜索

2024-06-20 04:38
文章标签 搜索 记忆 poj 1661

本文主要是介绍POJ 1661 记忆化搜索,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

测试数据:


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;elsedp[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;elsedp[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;
}




这篇关于POJ 1661 记忆化搜索的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077121

相关文章

Python使用DeepSeek进行联网搜索功能详解

《Python使用DeepSeek进行联网搜索功能详解》Python作为一种非常流行的编程语言,结合DeepSeek这一高性能的深度学习工具包,可以方便地处理各种深度学习任务,本文将介绍一下如何使用P... 目录一、环境准备与依赖安装二、DeepSeek简介三、联网搜索与数据集准备四、实践示例:图像分类1.

C# ComboBox下拉框实现搜索方式

《C#ComboBox下拉框实现搜索方式》文章介绍了如何在加载窗口时实现一个功能,并在ComboBox下拉框中添加键盘事件以实现搜索功能,由于数据不方便公开,作者表示理解并希望得到大家的指教... 目录C# ComboBox下拉框实现搜索步骤一步骤二步骤三总结C# ComboBox下拉框实现搜索步骤一这

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];