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. sublime安装

    1.sublime下载安装 下载地址:http://www.sublimetext.com/3 选择合适版本下载 2.自定义快捷键 Ctrl+Shift+P调出命令面板 3.自定义设置 Ctrl+Sh ...

  2. 如何将List<string>转化为string

    Convert List, string. A List can be converted to a string. This is possible with the ToArray method ...

  3. SAP中日期时间函数总结

    1.获得最后一天CALL FUNCTION 'FIMA_DATE_CREATE'  EXPORTING   I_DATE                       = I_DATE "输入 ...

  4. ST05 跟踪SQL

          SAP R/3 提供标准ABAP SQL 跟踪工具.使用T-Code:ST05 可以进入追踪设定画面:          在Trace Modes 区域中选择需要在SAP R/3 Serv ...

  5. Office 365 – SharePoint 2013 Online 中添加域和域名

    1.在SharePoint Online管理中心,点击菜单上的添加域,如下图: 2.进入管理域的页面,点击添加域来添加我们自己的域名,如下图: 3.进入“在 Office 365中添加新域”的向导,跟 ...

  6. SharePoint 向多行文本类型字段插入特殊类型链接

    1.在测试列表中插入一个多行文本字段,名字叫做Content,如下图: 2.在Content字段里,添加一个Link,如下图: 3.尝试输入Notes格式的Link,如下图: 4.点击OK的时候,弹出 ...

  7. linux集群运维工具:clustershell和pssh

    由于需要安装hadoop集群,有10台机器需要安装,一开始打算用SCP复制,后来觉得不可接受(实际现场可能数倍的机器集群,就是10台也不想干).后来在网上找了,发现了clustershell和pssh ...

  8. openssh/ntp/ftp漏洞

    这3种漏洞常规加固都要对应操作系统打官方漏洞升级包.既然这么说那下面就是不常规的: Openssh: 改ssh版本:whereis ssh //查看ssh目录cd 到该目录cp ssh ssh.bak ...

  9. 对抽屉效果几大github第三方库的调研

    在公司项目新版本方案选择中,对主导航中要使用的抽屉效果进行了调研.主要原因是旧的项目中所用的库ECS评价不是很好.现对当下比较火的几大热门抽屉效果的第三方库进行了调研.代码全部选自github 如果你 ...

  10. C中的流程控制

    一. 流程控制 l 顺序结构:默认的流程结构.按照书写顺序执行每一条语句. l 选择结构:对给定的条件进行判断,再根据判断结果来决定执行哪一段代码. l 循环结构:在给定条件成立的情况下,反复执行某一 ...