【万能的搜索,用广搜来解决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*启 ...
随机推荐
- Cordova各个插件使用介绍系列(二)—$cordovaBarcodeScanner扫描二维码与生成二维码
详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-2-cordovabarcodescanner/ 这是 ...
- (原创)用JAX-WS+Spring实现简单soap规范的webservice
转载请注明出处:http://www.cnblogs.com/Starshot/p/7050084.html Soap即简单对象访问协议,也可理解为一种用于程序之间通讯的规范,它主要基于XML和htt ...
- js模版引擎开发实战以及对eval函数的改进
简介 前段时间,想着自己写一个简单的模版引擎,便于自己平时开发demo时使用,同时也算是之前学习的知识的一种总结吧! 首先我们先了解一下模版引擎的工作原理吧! 1. 模版引擎其实就是将指定标签的内容根 ...
- 最全Oracle环境搭建之.NET程序员初遇Oracle
前言:如果你习惯了傻瓜式的一步步安装,那么Oracle和.NET搭配,绝对会让你头痛不已. 目前我不敢保证自己理解的Oracle理论部分100%正确,但环境安装过程一定可以收藏以备不时之需. 路这么长 ...
- SQL检测开始日期 结束日期 是否存在交叉
检测开始日期 结束日期 是否存在交叉 "+tj+" and ((starttime>="+starttime+" and starttime<=&q ...
- Pycharm创建的virtualenv环境缺失pip.exe的问题(Windows系统)
Windows环境: 1. Python安装在d:\Python\Python35下, Python新版本安装时默认会勾选pip功能 2. PyCharm的Settings中Create Virtua ...
- [leetcode-560-Subarray Sum Equals K]
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 【LeetCode】258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- Example005控制弹出窗口居中显示
<!-- 实例005控制弹出窗口居中显示 --> <head> <meta charset="UTF-8"> </head> < ...
- Loadrunner--自动关联和手动关联
2017-06-09 15:32:45个人也属于刚刚开始学习,有什么不对的地方敬请指导:qq:389791447 一开始的时候,准备去学习怎么去关联.一时也毛不着头脑,就在网上找了一些视频看,有的人说 ...