【bfs && 如何想到bfs】2014 Benelux Algorithm B
来源:互联网 发布:外设马淘宝 编辑:程序博客网 时间:2024/05/16 02:07
Problem Description
输入T,代表有T组测试数据。每组测试数据输入n, t代表有n个按钮,煮好食物需要t秒以上。接下来n个数,代表每个按钮能够增加烹饪的时间。问你煮好食物至少需要按多少次按钮,至少超出几秒。优先接近t秒,秒数一样,再考虑按钮次数少的。
思路:
一开始也有思考到bfs, 第一反应认为状态太多,是不是应该有目的性搜索,敲个A*,但是自己A*不稳,没敲过几次。所以就放弃了bfs的念头,转头思考dp,楞是想了一个多小时,也不知道如何做。比赛结束看了下AC人的代码,用的bfs,自己在好好思考了一下状态也就只有3600个,bfs速度很快。发博客给自己警记,第二次把最短路类型的题,思考dp,而且还没思考出来。总之这种类型的题,就是思考正确如何做的点了,就很简单,思考不到就GG.
#include<bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3fint a[20], n, t;int dist[5000];//0-3600个状态,所以不会超时。void bfs(){ int i; memset(dist, inf, sizeof(dist)); dist[0] = 0; queue<int> q; q.push(0); while(!q.empty()) { int u = q.front(); q.pop(); for(i = 0; i < n; i++) { int to = u + a[i]; if(to < 0) to = 0;//因为t的范围0-3600所以超过范围的值直接变为0 or 3600即可 if(to > 3600) to = 3600; if(dist[to] > dist[u] + 1)//更新目的点 { dist[to] = dist[u] + 1; q.push(to);//入队列 } } } for(i = t; i <= 3600; i++)//找到t后面,能通过按钮到达的时间。 { if(dist[i] != inf) break; } printf("%d %d\n", dist[i], i - t);}int main(){ int T; scanf("%d", &T); while(T--) { scanf("%d %d", &n, &t); for(int i = 0; i < n; i++) scanf("%d", &a[i]); if(t == 0) {//0秒的时候,不需要按,直接输出就好了 printf("0 0\n"); continue; } bfs(); } return 0;}
阅读全文
0 0
- 【bfs && 如何想到bfs】2014 Benelux Algorithm B
- 2014 Benelux Algorithm Programming Contest (BAPC 14)
- 【algorithm】BFS实例一
- 2014 Benelux Algorithm Programming Contest (BAPC 14) 部分题解
- 2014 Benelux Algorithm Programming Contest (BAPC 14) K
- Problem B bfs
- B - Prime Path(BFS)
- bfs--Codeforces,520B.
- codeforce 689B 【bfs】
- codeforces 320B bfs
- Problem B (BFS)
- Codeforces 329B bfs
- BFS B题
- BFS
- bfs
- BFS
- BFS
- bfs
- JAVAEE企业级应用开发浅谈之二:MVC和三层架构
- 六大设计原则之依赖倒置原则
- Unity3d 简单的背包系统(物品交换)
- 编程判断是C源文件还是C++源文件
- 1003. Emergency (25)
- 【bfs && 如何想到bfs】2014 Benelux Algorithm B
- PAT-甲级-1001 A+B Format
- 浅谈return
- python dict和set
- HDU5421-Victor and String
- scrapy爬虫-爬取慕课网全部课程
- 049 极值及最大值,最小值
- windows系统使用小技巧
- 请定义一个交通工具(Vehicle)的类,其中有: