题目

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.

输入格式

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 ≤ u, v < 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.

输出格式

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.

输入样例

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

输出样例

170

impossible

题解

我们把点和在该点拥有的油量看做一个状态,建立分层图

每个点向其相邻的油量减少量为路程长的点连边【费用0】,向其自身油量+1的点连边【费用为油价】

跑dijkstra【可能SPFA跑分层图什么的会快一些】

复杂度\(O(q * n * c * log(n + c))\)

注意每一只加1升油,不能随意加油,否则就会退化到\(O(q*n*c^2*log(n + c))\)

  1. #include<iostream>
  2. #include<cmath>
  3. #include<queue>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<algorithm>
  7. #define LL long long int
  8. #define REP(i,n) for (int i = 1; i <= (n); i++)
  9. #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
  10. #define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
  11. using namespace std;
  12. const int maxn = 1005,maxm = 20005,INF = 1000000000;
  13. inline int read(){
  14. int out = 0,flag = 1; char c = getchar();
  15. while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
  16. while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
  17. return out * flag;
  18. }
  19. int n,m,h[maxn],ne = 2;
  20. struct EDGE{int to,nxt,w;}ed[maxm];
  21. void build(int u,int v,int w){
  22. ed[ne] = (EDGE){v,h[u],w}; h[u] = ne++;
  23. ed[ne] = (EDGE){u,h[v],w}; h[v] = ne++;
  24. }
  25. int d[maxn][105],p[maxn],vis[maxn][105];
  26. struct node{int u,c,f;};
  27. inline bool operator <(const node& a,const node& b){return a.f > b.f;}
  28. priority_queue<node> q;
  29. int solve(int S,int T,int C){
  30. for (int i = 1; i <= n; i++)
  31. for (int j = 0; j <= C; j++)
  32. d[i][j] = INF,vis[i][j] = 0;
  33. d[S][0] = 0;
  34. q.push((node){S,0,d[S][0]});
  35. int v; node u;
  36. while (!q.empty()){
  37. u = q.top(); q.pop();
  38. if (vis[u.u][u.c]) continue;
  39. vis[u.u][u.c] = true;
  40. Redge(u.u)
  41. if (ed[k].w <= u.c && !vis[to = ed[k].to][v = u.c - ed[k].w] && d[to][v] > d[u.u][u.c]){
  42. d[to][v] = d[u.u][u.c]; q.push((node){to,v,d[to][v]});
  43. }
  44. if (!vis[u.u][u.c + 1] && d[u.u][u.c + 1] > d[u.u][u.c] + p[u.u]){
  45. d[u.u][u.c + 1] = d[u.u][u.c] + p[u.u];
  46. q.push((node){u.u,u.c + 1,d[u.u][u.c + 1]});
  47. }
  48. }
  49. int ans = INF;
  50. for (int i = 0; i <= C; i++) ans = min(ans,d[T][i]);
  51. return ans;
  52. }
  53. int main(){
  54. n = read(); m = read();
  55. REP(i,n) p[i] = read();
  56. int a,b,w;
  57. while (m--){
  58. a = read() + 1; b = read() + 1; w = read();
  59. build(a,b,w);
  60. }
  61. int q = read(),S,T,C,ans;
  62. while (q--){
  63. C = read(); S = read() + 1; T = read() + 1;
  64. ans = solve(S,T,C);
  65. if (ans == INF) puts("impossible");
  66. else printf("%d\n",ans);
  67. }
  68. return 0;
  69. }

UVA11367 Full Tank? 【分层图最短路】的更多相关文章

  1. poj3635Full Tank?[分层图最短路]

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

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

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

  3. BZOJ 2763 分层图最短路

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 撤销git pull命令

    比如:在master分支上执行了git pull命令,想回到pull之前分支所在的commit位置. 步骤一:用 git reflog master 查看master分支的历史变动记录,其中有一个就是 ...

  2. springboot(二十一)-集成memcached

    Memcached 介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站 ...

  3. npm模块安装机制简介

    npm是node的模块管理器,功能及其强大,它是node获得成功的重要原因之一. 正因为有了nom,我们只要一行命令,就能安装别人写好的模块. $ npm install 本文介绍npm模块安装机制的 ...

  4. 剑指offer22 栈的压入、弹出序列

    写的一个代码,虽然正确通过了,但我觉得会报vector越界的错误 class Solution { public: bool IsPopOrder(vector<int> pushV,ve ...

  5. echarts实现仪表盘(自己动起来,没有后端,顺便重温math.random

    let a = parseInt(Math.random() * (2 + 1), 10); let arr = []; arr.push(res[a]); let option = { toolti ...

  6. Java中的Static修饰符

    static(静态.修饰符):static修饰成员变量时:static修饰成员变量时,那么该成员变量的数据就是一个共享的数据. 静态成员变量的访问方式:方式一: 使用对象进行访问. 对象.属性名 方式 ...

  7. 03_4_this关键字

    03_4_this关键字 1. this关键字 在类的方法定义中使用的this关键字代表使用该方法的对象的引用. 当必须指出当前使用方法的对象是谁时要使用this. 有时使用this可以处理方法中成员 ...

  8. 为什么要在函数内部声明 var that = this 呢

    看一个例子 $('#conten').click(function(){ //this是被点击的#conten var that =this; $('.conten').each(function() ...

  9. Sorted Union-freecodecamp算法题目

    Sorted Union 1.要求 写一个 function,传入两个或两个以上的数组,返回一个以给定的原始数组排序的不包含重复值的新数组. 换句话说,所有数组中的所有值都应该以原始顺序被包含在内,但 ...

  10. HDU-4848-Such Conquering

    这题就是深搜加剪枝,有一个很明显的剪枝,因为题目中给出了一个deadline,所以我们一定要用这个deadline来进行剪枝. 题目的意思是求到达每个点的时间总和,当时把题看错了,卡了好久. 剪枝一: ...