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. WTL汉化版2013.10.15

    汉化内容: 2013.10.15 版本:当前可下载Trunk最新版,wtl-code-467-trunk.zip 汉化内容: 1.应用向导的部分汉化,考虑到部分词汇的表述问题,只汉化无影响部分 2.资 ...

  2. ios-cocos2d游戏开发基础-CCLayer和Touch事件-开发笔记

    有时候在同一个场景里你需要多个CCLayer.你可以参照以下代码生成这样的场景 +(id) scene { CCScene* scene = [CCScene node]; CCLayer* back ...

  3. Android下高斯模糊的算法和demo

    采用纯java和RenderScript两种方式来做高斯算法. 也可以用NDK来做,想试试的可以参考: http://stackoverflow.com/questions/2067955/fast- ...

  4. 【Unity3D】自动寻路(Nav Mesh Agent组件)

    1.首先添加场景模型 2.为场景模型(寻路路径)添加NavMesh渲染,操作:Windows->Navigation->勾选Navigation Static选项->不勾选Gener ...

  5. 怎么制作生成苹果手机app应用的下载二维码图片

    原文网址:http://jingyan.baidu.com/article/8065f87ff654262331249886.html app store应用生成二维码操作步骤: 1.首先在MAC上的 ...

  6. vs 2005中解决找不到模板项

    开始-->所有程序-->Microsoft Visual Studio 2005-->Visual Studio Tools-->Visual Studio 2005 Comm ...

  7. MySQL基础之第5章 操作数据库

    假设已经登录 mysql-h localhost -uroot -proot 5.1.显示.创建.删除数据库 show databases;     显示所有的数据库 create database ...

  8. linux下网络排错与查看

    基本的故障排除错误 故障的排除一定是先简单后复杂的,有的人把上述的文件反复配置,就是上不了网,一直都认为是系统出了故障,想重装机子.结果发现原来是网线压根就没插上. 排错要慢慢的按部就班的来: (1) ...

  9. HDU 5749 Colmerauer 单调队列+暴力贡献

    BestCoder Round #84   1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...

  10. Apache OFBiz 学习笔记 之 服务引擎 二

    加载服务定义文件   ofbiz-component.xml:所有的服务定义文件在每个组件的ofbi-component.xml文件中   加载服务定义 例:framework/common/ofbi ...