POJ-3635 Full Tank? (记忆化广搜)
Description
After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?
To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.
Input
The first line of input gives 1 ≤ n ≤ 1000 and 0 ≤ m ≤ 10000, the number of cities and roads. Then follows a line with n integers 1 ≤ pi ≤ 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 ≤ u, v < n and 1 ≤ d ≤ 100, telling that there is a road between u and v with length d. Then comes a line with the number 1 ≤ q ≤ 100, giving the number of queries, and q lines with three integers 1 ≤ c ≤ 100, s and e, where c is the fuel capacity of the vehicle, s is the starting city, and e is the goal.
Output
For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or "impossible" if there is no way of getting from s to e with the given car.
Sample Input
5 5
10 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4
Sample Output
170
impossible 题目大意:n个城市,m条公路构成了一张n个顶点,m条无向边的图。现在要从一个城市驾车到另一个城市,每单位油能走1公里路,已知每个城市的油价(元/单位)和车的油箱容量,问到达目的地的最小花费是多少。
题目分析:以当前的所在城市和油箱剩余油量作为状态参数,最多有 城市数x油箱容量=10^5 个状态,暴力是可以接受的,又让求最小花费,就敲定用广搜了。现在考虑状态转移,当到达一个城市时,无非面临了两种选择:1.当油量足够能通过下条路时,直接扩展;2.加一个单位油(加一个单位油没问题,如果加多的话,会重复,造成不必要的时间浪费)。问题看似已经解决,但如果仅仅是这样的话,会TLE。
解决办法就是将广搜的过程记忆化,当达到某个状态时解比已经得到的解更优时,搜索才继续进行下去,并更新最优解。要开辟新的数组来记录状态的最优解。 代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
const int INF=<<;
struct node
{
int pos,tank,m;
bool operator < (const node &a) const {
return m>a.m;
}
};
struct edge
{
int to,w,nxt;
};
edge e[];
int n,vis[][],dp[][],head[],price[],cnt;
void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
void bfs(int v,int s,int t)
{
priority_queue<node>q;
for(int i=;i<=n;++i)
for(int j=;j<=v;++j)
dp[i][j]=INF;
memset(vis,,sizeof(vis));
dp[s][]=;
node sta;
sta.m=,sta.pos=s,sta.tank=;
q.push(sta);
while(!q.empty()){
node u=q.top();
q.pop();
int pos=u.pos;
vis[pos][u.tank]=;
if(pos==t){
printf("%d\n",u.m);
return ;
}
for(int i=head[pos];i!=-;i=e[i].nxt){
if(u.tank>=e[i].w&&!vis[e[i].to][u.tank-e[i].w]&&dp[e[i].to][u.tank-e[i].w]>u.m){
dp[e[i].to][u.tank-e[i].w]=u.m;
node nxt;
nxt.pos=e[i].to,nxt.tank=u.tank-e[i].w,nxt.m=u.m;
q.push(nxt);
}
}
if(u.tank+<=v&&!vis[pos][u.tank+]&&dp[pos][u.tank+]>dp[pos][u.tank]+price[pos]){
dp[pos][u.tank+]=dp[pos][u.tank]+price[pos];
node nxt;
nxt.pos=pos,nxt.m=dp[pos][u.tank+],nxt.tank=u.tank+;
q.push(nxt);
}
}
printf("impossible\n");
}
int main()
{
int m,q,a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=;
for(int i=;i<n;++i)
scanf("%d",price+i);
memset(head,-,sizeof(head));
while(m--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
scanf("%d",&q);
while(q--){
scanf("%d%d%d",&c,&a,&b);
bfs(c,a,b);
}
}
return ;
}
POJ-3635 Full Tank? (记忆化广搜)的更多相关文章
- poj 3249 Test for Job (记忆化深搜)
http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissi ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 1088 滑雪(记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 92384 Accepted: 34948 Description ...
- POJ 1088 滑雪【记忆化搜索】
题意:给出一个二维矩阵,要求从其中的一点出发,并且当前点的值总是比下一点的值大,求最长路径 记忆化搜索,首先将d数组初始化为0,该点能够到达的路径长度保存在d数组中,同时把因为路径是非负的,所以如果已 ...
- POJ 3635 Full Tank? 【分层图/最短路dp】
任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 1088 滑雪 DFS 记忆化搜索
http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...
- poj 3278 Catch That Cow (广搜,简单)
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...
- poj 1088 滑雪_记忆化搜索
题意:略 直接用记忆化搜索就行了 #include<cstdio> #include<iostream> using namespace std; int n,m; int m ...
- POJ 2738 Two Ends(记忆化)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
随机推荐
- python之路----面向对象的封装特性
封装 [封装] 隐藏对象的属性和实现细节,仅对外提供公共访问方式. 广义上面向对象的封装 :代码的保护,面向对象的思想本身就是一种只让自己的对象能调用自己类中的方法 狭义上的封装 —— 面向对象的三大 ...
- Mysql管理工具 SqlYog快捷键大全
Ctrl+M 创建一个新的连接Ctrl+N 使用当前设置新建连接Ctrl+F4 断开当前连接 对象浏览器F5 刷新对象浏览器(默认)Ctrl+B 设置焦点于对象浏览器 SQL 窗口 ...
- python模块-random随机数模块
导入随机数模块import random 1.random.random() 生成[0,1)之间的随机小数 2.random.randint(a,b) 生成[a,b]之间的随机整数 3.random. ...
- Total Difference String
Total Difference Strings 给一个string列表,判断有多少个不同的string,返回个数相同的定义:字符串长度相等并从左到右,或从右往左是同样的字符 abc 和 cba 为视 ...
- 配置redis, make的时候: zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
今天正在centos7.3里面配置redis3.0, 结果make的时候抛出编译中断 CC adlist.o In file included from adlist.c:34:0: zmalloc. ...
- Educational Codeforces Round 21 Problem A - C
Problem A Lucky Year 题目传送门[here] 题目大意是说,只有一个数字非零的数是幸运的,给出一个数,求下一个幸运的数是多少. 这个幸运的数不是最高位的数字都是零,于是只跟最高位有 ...
- Maximum GCD (stringstream)题解
Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possiblepa ...
- ASP.NET MVC之Bundle压缩JS和CSS
介绍Bundle之前先引用<淘宝技术这十年>中一段话,对Web前端稍微有点常识的人都应该知道,浏览器下一步会加载页面中用到的CSS.JS(JavaScript).图片等样式.脚本和资源文件 ...
- 用python生成器实现杨辉三角
先看杨辉三角的形态: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 上学的时候大多是用c语言的两层for循环在实现,现在我们尝试用生成器来实现. 先说思路:我 ...
- Linux——帮助命令简单学习笔记
Linux帮助命令简单学习笔记: 一: 命令名称:man 命令英文原意:manual 命令所在路径:/usr/bin/man 执行权限:所有用户 语法:man [命令或配置文件] 功能描述:获得帮助信 ...