poj 3635 Full Tank? ( bfs+dp思想 )
|
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 Sample Output 170 Source |
题意:
给定n(n<=1000)个编号为0..n-1加油站的每单位油价格,再给定m(m<=10000)条边,并给出通过其所需油(<=100),询问q(q<=100)次,询问油箱容量为c(c<=100)时,s到e的所需最小费用。
思路:
dp[i][j]表示到达i点剩余油量为j时的最小花费,由于是在图上,没有确定的递推方向,那么用一个结构体{ u-当前位置、oil-剩余油量、cst-当前花费 },进行堆优化bfs就能解决问题了。
到达一个状态后看做有两种操作:
1.加一升油。
2.到达下一个节点。
那么所有的有效状态就能表示出来了,第一次到达e的费用即为最小花费。
ps:这种做法和优先队列优化的最短路很像的,只是状态多了一维。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 20005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.000001
using namespace std; int n,m,ans,cnt,cap,s,e;
int cost[maxn],pp[maxn];
bool vis[maxn][maxn];
int dp[maxn][maxn];
struct Node
{
int v,next,w;
} edge[MAXN];
struct node
{
int u,oil,cst;
bool operator <(const node &xx)const
{
return cst>xx.cst;
}
} cur,now;
priority_queue<node>q; void addedge(int u,int v,int w)
{
cnt++;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=pp[u];
pp[u]=cnt;
}
bool bfs()
{
int i,j,u,v,w,oil,cst;
memset(dp,0x3f,sizeof(dp));
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
cur.u=s;
cur.oil=0;
cur.cst=0;
dp[s][0]=0;
q.push(cur);
while(!q.empty())
{
now=q.top();
q.pop();
u=now.u;
oil=now.oil;
cst=now.cst;
if(u==e)
{
ans=cst;
return true ;
}
if(vis[u][oil]) continue ;
vis[u][oil]=1;
if(oil<cap)
{
cur.u=now.u;
cur.oil=oil+1;
cur.cst=cst+cost[u];
if(dp[cur.u][cur.oil]>cur.cst&&!vis[cur.u][cur.oil])
{
dp[cur.u][cur.oil]=cur.cst;
q.push(cur);
}
}
for(i=pp[u]; i; i=edge[i].next)
{
v=edge[i].v;
w=edge[i].w;
if(oil>=w&&dp[v][oil-w]>cst&&!vis[v][oil-w])
{
dp[v][oil-w]=cst;
cur.u=v;
cur.oil=oil-w;
cur.cst=cst;
q.push(cur);
}
}
}
return false ;
}
int main()
{
int i,j,t,u,v,w,q;
while(~scanf("%d%d",&n,&m))
{
for(i=0; i<n; i++)
{
scanf("%d",&cost[i]);
}
cnt=0;
memset(pp,0,sizeof(pp));
for(i=1; i<=m; i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
scanf("%d",&q);
for(i=1; i<=q; i++)
{
scanf("%d%d%d",&cap,&s,&e);
if(bfs()) printf("%d\n",ans);
else printf("impossible\n");
}
}
return 0;
}
poj 3635 Full Tank? ( bfs+dp思想 )的更多相关文章
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 Full Tank? 【分层图/最短路dp】
任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- poj 3635 Full Tank? ( 图上dp )
题意: 已知每一个点的加油站的油价单位价格(即点权).每条路的长度(边权). 有q个询问.每一个询问包含起点s.终点e和油箱容量. 问从起点走到终点的最小花费.假设不可达输出impossible,否则 ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- [POJ 3635] Full Tank?
题目 Description 已知每个点的加油站的油价单价(即点权),每条路的长度(边权). 有q个询问,每个询问包括起点s.终点e和油箱容量c. 问从起点走到终点的最小花费.如果不可达输出impos ...
- AcWing:176. 装满的油箱(bfs + dijiskla思想)
有N个城市(编号0.1…N-1)和M条道路,构成一张无向图. 在每个城市里边都有一个加油站,不同的加油站的单位油价不一样. 现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车 ...
- ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)
这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...
- ZOJ 3596Digit Number(BFS+DP)
一道比较不错的BFS+DP题目 题意很简单,就是问一个刚好包含m(m<=10)个不同数字的n的最小倍数. 很明显如果直接枚举每一位是什么这样的话显然复杂度是没有上限的,所以需要找到一个状态表示方 ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
随机推荐
- 面向函数范式编程(Functional programming)
函数编程(简称FP)不只代指Haskell Scala等之类的语言,还表示一种编程思维,软件思考方式,也称面向函数编程. 编程的本质是组合,组合的本质是范畴Category,而范畴是函数的组合. 首先 ...
- [转] jQuery Infinite Ajax Scroll(ias) 分页插件介绍
原文链接:http://justflyhigh.com/index.php/articlec/index/index.php?s=content&m=aticle&id=91 Infi ...
- 自学了三天的SeaJs学习,解决了前端的一些问题,与小伙伴们一起分享一下!
我为什么学习SeaJs? [第一]:为了解决项目中资源文件版本号的问题,以及打包压缩合并等问题. [第二]:好奇心和求知欲.[我发现很多知名网站也都在使用(qq空间, msn, 淘宝等等),而且 Se ...
- poj 1780 Code
//题目描述:KEY公司开发出一种新的保险箱.要打开保险箱,不需要钥匙,但需要输入一个正确的.由n位数字组成的编码.这种保险箱有几种类型,从给小孩子玩的玩具(2位数字编码)到军用型的保险箱(6位数字编 ...
- ZOJ 1455 Schedule Problem(差分约束系统)
// 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...
- zoj 1967 Fiber Network/poj 2570
题意就是 给你 n个点 m条边 每条边有些公司支持 问 a点到b点的路径有哪些公司可以支持 这里是一条路径中要每段路上都要有该公司支持 才算合格的一个公司// floyd 加 位运算// 将每个字符当 ...
- Android 使用库项目时的一个特殊tip
前提: 项目A作为库项目被项目B引用,但是项目A中有自定义的控件和自定义的属性,当在项目B中使用自定义的属性时,编译时就会直接报错:No resource identifier found for a ...
- Spring4整合Hibernate4
首先,要明确Spring整合Hibernate可以做什么? 答案是: 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使用上Spring的声明式事务 整 ...
- Android-AnimationDrawable(三)运行的几种方式
项目开发用到了AnimationDrawable,调用start后没有运行,很纳闷.google搜了下.记录一下. 这个AnimationDrawable.start不能直接写在onClick,onS ...
- 对Spring IoC容器实现的结构分析
本文的目标:从实现的角度来认识SpringIoC容器. 观察的角度:从外部接口,内部实现,组成部分,执行过程四个方面来认识SpringIoC容器. 本文的风格:首先列出SpringIoC的外部接口及内 ...