Full Tank?
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7248   Accepted: 2338

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


题意:有了油量限制,每个点加油价格不一样,对于每个询问求最少花费

一看到d[i][j]这样的状态表示,马上想到分层图最短路,有点类似DP的思想
按油量分层,d[i][j]表示到节点i还有j个油的最小花费(不是最短路)
两种决策,加一个油或者直接走
感觉用Dijkstra写比较好
注意状态判重,多判一次也无所谓了
 
小优化:离线处理询问,相同起点同时处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e3+,M=1e4+,V=;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
struct edge{
int v,ne,w;
}e[M<<];
int h[N],cnt=;
void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int n,m,Q,u,v,w,p[N],c,st,ed; struct hn{
int u,d,f;
bool operator <(const hn &rhs)const{return d>rhs.d;}
hn(int a=,int b=,int c=):u(a),d(b),f(c){}
};
int d[N][V];
bool done[N][V];
void bfs(int c,int st,int ed){
memset(d,,sizeof(d));
memset(done,,sizeof(done));
priority_queue<hn> q;
q.push(hn(st,,));
d[st][]=; while(!q.empty()){
hn x=q.top();q.pop();
int u=x.u,f=x.f,cost=x.d;//printf("bfs %d %d\n",u,f);
//if(done[u][f]) continue;
done[u][f]=;
if(u==ed){printf("%d\n",cost);return;} if(f+<=c&&!done[u][f+]&&d[u][f]+p[u]<d[u][f+]){
d[u][f+]=d[u][f]+p[u];
q.push(hn(u,d[u][f+],f+));
}
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(f>=w&&!done[v][f-w]&&d[v][f-w]>cost){
d[v][f-w]=cost;
q.push(hn(v,cost,f-w));
}
}
}
printf("impossible\n");
} int main(){
n=read();m=read();
for(int i=;i<n;i++) p[i]=read();
for(int i=;i<=m;i++){
u=read();v=read();w=read();
ins(u,v,w);
}
Q=read();
for(int i=;i<=Q;i++){
c=read();st=read();ed=read();
bfs(c,st,ed);
}
}
 

poj3635Full Tank?[分层图最短路]的更多相关文章

  1. HDU 5669 线段树优化建图+分层图最短路

    用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...

  2. BZOJ 2763 分层图最短路

    突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...

  3. 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)

    [题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...

  4. 【网络流24题】 No.14 孤岛营救问题 (分层图最短路)

    [题意] 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛, 营救被敌军俘虏的大兵瑞恩. 瑞恩被关押在一个迷宫里, 迷宫地形复杂, 但幸好麦克得到了迷宫的地形图. 迷宫的外形是 ...

  5. BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路

    BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路 Description “我要成为魔法少女!”     “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切, ...

  6. BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路

    BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M ...

  7. Nowcoder contest 370H Rinne Loves Dynamic Graph【分层图最短路】

    <题目链接> 题目大意:Rinne 学到了一个新的奇妙的东西叫做动态图,这里的动态图的定义是边权可以随着操作而变动的图.当我们在这个图上经过一条边的时候,这个图上所有边的边权都会发生变动. ...

  8. ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】

    <题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...

  9. BZOJ2662[BeiJing wc2012]冻结——分层图最短路

    题目描述 “我要成为魔法少女!”     “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„”     在这个愿望被实现以后的世界里,人们享受着魔法卡片(Spe ...

随机推荐

  1. php 过滤英文标点符号 过滤中文标点符号

    php 过滤英文标点符号 过滤中文标点符号 代码 function filter_mark($text){ if(trim($text)=='')return ''; $text=preg_repla ...

  2. [deviceone开发]-HeaderView和FooterView的示例

    一.简介 这个是利用do_ListView组件实现下拉和上拉刷新功能的例子,除了do_ListView,其它比如do_Webview,do_ScrollView都有这个个功能.对应的BBS里的帖子详细 ...

  3. SharePoint 2013 VSS 编写器

    Windows Server 包含的 VSS 是提供内置卷影复制功能的基础结构.VSS 创建的卷影副本扩展了存储管理员的磁带备份存档解决方案,提供可轻松.有效创建和还原的高保真时间点副本,从而帮助简化 ...

  4. Swift安装

    Server1 .Update sudo apt-get update sudo apt-get upgrade . sudo apt-get install bridge-utils .IP 3.1 ...

  5. sharepoint 弹出窗口

    function openAnswerQandADialog(aUrl,aTitle) { var options = { url: aUrl, width: 1024, height: 768, t ...

  6. iOS常用手势识别器

    手势识别状态: typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { // 没有触摸事件发生,所有手势识别的默认状态 UIGestureReco ...

  7. Android数据存储五种方式总结

    本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据     2 文件存储数据       3 SQLite数据库存储数据 4 使用Cont ...

  8. socket编程中客户端常用函数 以及简单实现

    1 常用函数 1.1   connect() int connect(int sockfd, const struct sockaddr *servaddr, socklen_taddrlen); 客 ...

  9. java反射机制一个例子

    import java.lang.reflect.*; public class DumpMethods { public static void main(String args[]) { try ...

  10. 百度编辑器ueditor 异步加载时,初始化没办法赋值bug解决方法

    百度编辑器ueditor 异步加载时,初始化没办法赋值bug解决方法 金刚 前端 ueditor 初始化 因项目中使用了百度编辑器——ueditor.整体来说性能还不错. 发现问题 我在做一个编辑页面 ...