【万能的搜索,用广搜来解决DP问题】ZZNU -2046 : 生化危机 / HDU 1260:Tickets
2046 : 生化危机
时间限制:1 Sec内存限制:128 MiB
提交:19答案正确:8
题目描述
输入
输出
样例输入
1
2
1 3
4
样例输出
20:00:04
提示
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> //这两个题存在惊人的相似程度, 原因其实也很简单--你想
#include<queue>
using namespace std;
int k,n,w[],a[],b[],ans[];
struct node
{
int x;
int step;
};
void bfs() ;
int main()
{
int i,T;
cin>>T;
while(T--)
{
memset(ans,,sizeof(ans));k=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
cin>>n;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n-;i++)
scanf("%d",&b[i]);
bfs();
sort(ans,ans+k);
int t=ans[];
int h=,m=,s=;
h=(h+t/)%;
m=(t/)%;
s=t%;
printf("%02d:%02d:%02d\n",h,m,s);
}
return ;
} void bfs() //激活下面的注释区,你将看到整个搜索的大致过程
{
memset(w,,sizeof(w));
queue<node>Q;
struct node q,p;
q.x=;q.step=;
Q.push(q);// cout<<q.x<<" "<<q.step<<" ";
while( Q.size()> )
{
p=Q.front();
Q.pop();
for(int i=;i<=;i++)
{
if(p.step>=n)
{
ans[k++]=p.x;//cout<<p.x<<" "<<p.step<<" ";
continue;
}
if(i==&&(p.step+<=n))
{
q.x=p.x+ a[p.step+];q.step=p.step+;
}
else if((i==)&&(p.step+<=n))
{
q.x=p.x+ b[p.step+];q.step=p.step+;
}
else{} // cout<<i<<" q.x: "<<q.x<<" q.step: "<<q.step<<endl;
Q.push(q); }
} return ;
}
总结:学长们说的很对,搜索是万能的!如果题目不要求时间复杂度,不要求内存,理论上任何一个题目都是可以做出来的。hdu上的那题需要更改一下,有兴趣的同学们可以试试!其实这是一道DP水题!!
【杭电的原题链接 1260:Tickets 】http://acm.hdu.edu.cn/showproblem.php?pid=1260
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring> // HDU 1260:Tickets
using namespace std;
int a[],b[],dp[],n;
int main()
{
int i,j,T,k,m;
cin>>T;
while(T--)
{
memset(a,,sizeof(a));memset(b,,sizeof(b));
memset(dp,,sizeof(dp));
cin>>n;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n-;i++)
scanf("%d",&b[i]);
dp[]=a[];
for(i=;i<=n;i++)
{
dp[i]=min(dp[i-]+a[i],dp[i-]+b[i-]);
}
m=dp[n];
int hh=,mm=,ss=;
hh=(hh+m/)%;m=m%;
mm=m/;
ss=m%;
printf("%02d:%02d:%02d",hh,mm,ss);
if(hh<)
printf(" am\n");
else
printf(" pm\n");
} return ;
}
你懂了吧!傻眼了吧!
【万能的搜索,用广搜来解决DP问题】ZZNU -2046 : 生化危机 / HDU 1260:Tickets的更多相关文章
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Oj 24260: Lilypad Pond (神奇广搜题,状态搜索)
题目 为了让奶牛们娱乐和锻炼,约翰建造了一个美丽的池塘.这个池塘是矩形的,可以分成M×N个方格.一些格子是坚固得令人惊讶的莲花,还有一些是岩石,其余的只是美丽,纯净,湛蓝的水.贝西正在练习芭蕾舞,她站 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- POJ-3635 Full Tank? (记忆化广搜)
Description After going through the receipts from your car trip through Europe this summer, you real ...
- E. New Reform_贪心,深搜,广搜。
E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
随机推荐
- postgis 中的距离计算
最近在做一个项目,有一个功能想要实现类似于查询附近的人的功能.由于项目的原因数据库只能使用 postgresql,空间查询就使用了 postgis 来实现. 具体业务像这样:业务需要返回附近距自己 1 ...
- 使用mongodb作为Quartz.Net下的JobStore实现底层的持久化机制
我们都知道默认的Quartz底层采用的是RAMJobStore,所有的Job,Trigger,Calendar都是用Dictionary,SortSet等等这样的数据结构进行储存,相对来说性能肯 定快 ...
- java实现Excel的导入、导出
一.Excel的导入 导入可采用两种方式,一种是JXL,另一种是POI,但前者不能读取高版本的Excel(07以上),后者更具兼容性.由于对两种方式都进行了尝试,就都贴出来分享(若有错误,请给予指正) ...
- PS-前端切图教程(切jpg图和切png图)
微微一运功,把家底都抖出来了. 不过,作为一个设计出身的前端来说,摸ps就和摸键盘一样了 所以可能教程中还是有没用过ps的人看不懂的地方, 欢迎加群讨论:613512106... ---------- ...
- jQuery.merge( first, second )返回: Array
jQuery.merge( first, second )返回: Array描述: 合并两个数组内容到第一个数组.first类型: Array第一个用于合并的数组,其中将会包含合并后的第二个数组的内容 ...
- [leetcode-494-Target Sum]
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
- 【Android Developers Training】 87. 序言:同步到云
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- android怎么输出信息到logcat
- 在ASP.NET CORE 2.0使用SignalR技术
一.前言 上次讲SignalR还是在<在ASP.NET Core下使用SignalR技术>文章中提到,ASP.NET Core 1.x.x 版本发布中并没有包含SignalR技术和开发计划 ...
- Java泛型概念
1. 概述在引入范型之前,Java类型分为原始类型.复杂类型,其中复杂类型分为数组和类.引入范型后,一个复杂类型就可以在细分成更多的类型.例如原先的类型List,现在在细分成List<Objec ...