Full Tank?
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5857   Accepted: 1920

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 ≤ uv < 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

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思想 )的更多相关文章

  1. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  2. POJ 3635 Full Tank? 【分层图/最短路dp】

    任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. poj 3635 Full Tank? ( 图上dp )

    题意: 已知每一个点的加油站的油价单位价格(即点权).每条路的长度(边权). 有q个询问.每一个询问包含起点s.终点e和油箱容量. 问从起点走到终点的最小花费.假设不可达输出impossible,否则 ...

  4. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  5. [POJ 3635] Full Tank?

    题目 Description 已知每个点的加油站的油价单价(即点权),每条路的长度(边权). 有q个询问,每个询问包括起点s.终点e和油箱容量c. 问从起点走到终点的最小花费.如果不可达输出impos ...

  6. AcWing:176. 装满的油箱(bfs + dijiskla思想)

    有N个城市(编号0.1…N-1)和M条道路,构成一张无向图. 在每个城市里边都有一个加油站,不同的加油站的单位油价不一样. 现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车 ...

  7. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

  8. ZOJ 3596Digit Number(BFS+DP)

    一道比较不错的BFS+DP题目 题意很简单,就是问一个刚好包含m(m<=10)个不同数字的n的最小倍数. 很明显如果直接枚举每一位是什么这样的话显然复杂度是没有上限的,所以需要找到一个状态表示方 ...

  9. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

随机推荐

  1. android动画坐标定义

    这段时间一直在忙Android的项目,总算抽出点时间休息一下,准备把一些项目用到的Android经验分享一下. 在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍一下 ...

  2. iOS开发:mac使用svn管理项目

    记录mac下常用的svn命令: 1.检出项目: svn checkout .../svn/projectName --username=xxx --password=xxx //将ip换成svn服务器 ...

  3. HDU 1695 (莫比乌斯反演) GCD

    题意: 从区间[1, b]和[1, d]中分别选一个x, y,使得gcd(x, y) = k, 求满足条件的xy的对数(不区分xy的顺序) 分析: 虽然之前写过一个莫比乌斯反演的总结,可遇到这道题还是 ...

  4. js自动判断浏览器类型跳转到手机版

    //电脑版头部写法:<script language="javascript"> function is_mobile() { var regex_match = /( ...

  5. json转csv

    import re # csv格式 # 'k1,k2,k3\nv1,v2,v3\nv4,v5,v6\n' market_list_data = { "data": [ { &quo ...

  6. 动态加载so文件

    在开发过程中,经常会用到第三方库,比如地图.视频.文档编辑.图表之类.依赖这些库,需要添加其SDK,有时需要用到jni层的So文件,比如百度地图等. 那么问题来了,如果两个不同的库之间的so文件发生冲 ...

  7. Android-使用getIdentifier()获取资源Id

    使用getIdentifier()获取资源Id int i= getResources().getIdentifier("icon", "drawable", ...

  8. JavaScript 现状:方言篇

    导读 JavaScript 和其他编程语言有一个很大的不同,它不像单纯的一个语言,而像一个由众多方言组成大家族.从 2009 年 CoffeeScript 出现开始,近几年出现了大量基于 JavaSc ...

  9. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 page_should_contain_list(self, locator, message='', loglevel='INFO')

    def page_should_contain_list(self, locator, message='', loglevel='INFO'): """Verifies ...

  10. 将VLC库封装为duilib的万能视频播放控件

    转载请说明出处,谢谢~~ 昨天封装好了基于webkit的浏览器控件,修复了duilib的浏览器功能的不足,而我的仿酷狗播放器项目中不光需要浏览器,同时也需要视频播放功能,也就是完成MV的功能.所以我打 ...