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. 定制Eclipse IDE之插件篇(一)

    上文回顾:定制Eclipse IDE之功能篇(二) 在这篇文章中,我会将我定制eclipse用到的其他插件罗列出来. 一.汉化插件 Eclipse本身是英文显示的,我们能够通过插件汉化.  1. 选择 ...

  2. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q75-Q77)

    Question 75You are designing a feature for a SharePoint 2010 solution that will be activated by defa ...

  3. android键盘弹出头部上移处理

    <ScrollView android:id="@+id/top_bar" android:layout_width="fill_parent" andr ...

  4. App 即时通讯 SDK

    1.网易云信 http://netease.im/ 2.环信 http://www.easemob.com/customer/im 3.融云 http://www.rongcloud.cn/ 4.极光 ...

  5. 打印完整URL

    if(requestDictionary != nil) { //添加参数,将参数拼接在url后面 NSMutableString *paramsString = [NSMutableString s ...

  6. 【Android自定义控件】支持多层嵌套RadioButton的RadioGroup

    前言 非常喜欢用RadioButton+RadioGroup做Tabs,能自动处理选中等效果,但是自带的RadioGroup不支持嵌套RadioButton(从源码可看出仅仅是判断子控件是不是Radi ...

  7. WPF 使用Caliburn.Micro 多线程打开窗口

    我们都知道在WPF里面用多线程打开一个窗口很简单.如下 public void ClickMe(object sender) { Thread newWindowThread = new Thread ...

  8. 数据存储与IO(一)

    应用程序沙盒简介:iOS应用程序只能在系统为它分配的文件区域内读写文件,这个区域就是此应用程序的沙盒,Application目录下的GUID文件夹就是沙盒,这个文件夹是系统随机命名的.程序所有的非代码 ...

  9. HTML5 respond.js 解决IE6~8的响应式布局问题

    HTML5 respond.js 解决IE6~8的响应式布局问题   响 应式布局,理想状态是,对PC/移动各种终端进行响应.媒体查询的支持程度是IE9+以及其他现代的浏览器,但是IE8在市场当中仍然 ...

  10. 利用PHPMailer 来完成PHP的邮件发送

    翻起之前的代码看了一下,还是发表到这里,以后容易查找. 以下的两个文件在这里下载 http://download.csdn.net/detail/u013085496/9673828 也可以直接上gi ...