Full Tank? POJ - 3635 (bfs | 最短路)

时间:2021-12-15 11:49:06

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 ≤ 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.

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 题意:n个城市,m条路,每个城市有各自的油价,每条路有各自需要的油量,q个问题,每个问题给出c(汽车油箱容积)、s(起始起点)、t(出发地点),问你最小的邮费
思路:对于某的城市, 当 当前油量 小于 c时,可以选择加油,也就是油费+单位油费,油量+1,城市不变;同时如果 当前油量 >= 某条路花费,就可以通过这路去其他城市,这样油费不变,油量-路上油耗,城市变成另一个城市。
因为我们需要一个油费最少的答案,所有需要用优先队列来保证bfs的单调性,也就是最短路,这样当一个城市第一次出队列的时候就是它的最小油费
    #include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
int n,m,q,c,s,t;
typedef pair<int,int> p;
typedef pair<int,pair<int,int> >mp;
int city[1005];
int next[1005];
int cnt;
int vis[1005][105];
struct Node
{
int x,y,val;
int next;
Node(int x=0,int y=0,int val=0,int next=-1):x(x),y(y),val(val),next(next) {}
} node[20005]; void add(int x,int y,int val)
{
node[++cnt].x = x;
node[cnt].y = y;
node[cnt].val = val;
node[cnt].next = next[x];
next[x] = cnt;
} int bfs(int s,int t)
{
memset(vis,0x3f,sizeof(vis));
priority_queue<mp,vector<mp>,greater<mp> >que;
while(!que.empty())
que.pop();
que.push(mp(0,p(0,s)));
while(!que.empty())
{
mp tmp = que.top();
que.pop();
int now = tmp.second.second;
int s = tmp.second.first;
if(vis[now][s] != 0x3f3f3f3f)
continue;
vis[now][s] = tmp.first;
if(now == t)
return s;
if(s < c)
que.push(mp(vis[now][s]+city[now],p(s+1,now)));
for(int i=next[now]; i!=-1; i=node[i].next)
{
int w = node[i].val;
int To = node[i].y;
if(w <= s && vis[now][s] < vis[To][s-w])
{
que.push(mp(vis[now][s],p(s-w,To)));
}
}
}
return -1;
} int main()
{
scanf("%d%d",&n,&m);
memset(next,-1,sizeof(next));
cnt = 0;
for(int i=0; i<n; i++)
{
scanf("%d",&city[i]);
}
for(int i=1; i<=m; i++)
{
int u,v,val;
scanf("%d%d%d",&u,&v,&val);
add(u,v,val);
add(v,u,val);
}
scanf("%d",&q);
for(int i=1; i<=q; i++)
{
scanf("%d%d%d",&c,&s,&t);
int flag = bfs(s,t);
if(flag != -1)printf("%d\n",vis[t][flag]);
else
printf("impossible\n");
}
}

Full Tank? POJ - 3635 (bfs | 最短路)的更多相关文章

  1. poj 3635&lpar;bfs&plus;优先队列&rpar;

    题目链接:http://poj.org/problem?id=3635 思路:本题主要运用的还是贪心思想,由于要求st->ed的最小花费,那么每经过一个城市,能不加油就尽量不加油,用dp[i][ ...

  2. POJ 3635 - Full Tank&quest; - &lbrack;最短路变形&rsqb;&lbrack;手写二叉堆优化Dijkstra&rsqb;&lbrack;配对堆优化Dijkstra&rsqb;

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  3. POJ 3635 Full Tank&quest; 【分层图&sol;最短路dp】

    任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. POJ 2251 Dungeon Master &lpar;BFS最短路&rpar;

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  5. 【bzoj5049】&lbrack;Lydsy九月月赛&rsqb;导航系统 并查集&plus;双向BFS最短路

    题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...

  6. 【bzoj1189】&lbrack;HNOI2007&rsqb;紧急疏散evacuate BFS最短路&plus;动态加边网络流

    题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...

  7. BZOJ 1195 &lbrack;HNOI2006&rsqb;最短母串 &lpar;Trie图&plus;状压&plus;bfs最短路&rpar;

    BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...

  8. POJ 1161 Walls&lpar;最短路&plus;枚举&rpar;

    POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了 ...

  9. UVa 1600 Patrol Robot &lpar;BFS最短路 &amp&semi;&amp&semi; 略不一样的vis标记&rpar;

    题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...

随机推荐

  1. Java各种排序算法详解

    排序大的分类可以分为两种:内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.下面讲的排序都是属于内排序. 内排序有可以分为以下几类: (1).插 ...

  2. Ogre初入手:最简单的ogre程序骨架

    本文内容主要参考于页面 http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Ogre+Wiki+Tutorial+Framework Ogre是一个非 ...

  3. c 指针兼容性问题

    指针兼容性问题: const指针不能赋值给非const指针. 非const指针可以赋值给const 指针,但前提是只是一层间接运算 Example: int *pt1; const *pt2; con ...

  4. 关于内存的5个函数(malloc&comma;VirtualAlloc&comma;GlobalAlloc&comma;LocalAlloc&comma;HeapAlloc)

    VirtualAlloc 该函数的功能是在调用进程的虚地址空间,预定或者提交一部分页,如果用于内存分配的话,并且分配类型未指定MEM_RESET,则系统将自动设置为0 一次分配 1PAGE 以上的 R ...

  5. CentOS 6&period;2 中文

    在虚拟机里面安装好centos6.2之后,默认是英文!   对于命令行操作无所谓啦,但是如果想看界面,就不是很适应!   修改方法如下:   1.用root登录系统,密码为创建虚拟机时候的密码.创建虚 ...

  6. v-model

    仅用于以下控件: <input> <select> <textarea> 组件 v-model以Vue 实例的数据作为数据来源,应当在组件的 data 选项中声明初 ...

  7. 外贸建站之图片预加载JS代码分享

    外贸建站之图片预加载JS代码分享 function preloadimg() { setTimeout(function() { new Image().src = "images/2017 ...

  8. MySQL中group by &comma; sum &comma; case when then 的使用

    在我们使用数据库的时候,可能会遇到需要进行统计的情况. 比如需要统计一下,下表中各个年份的胜负场数. 遇到这样的情况,我们应该怎么办呢? 在mysql中我们可以使用group by sum  case ...

  9. kafka系列一、kafka安装及部署、集群搭建

    一.环境准备 操作系统:Cent OS 7 Kafka版本:kafka_2.10 Kafka官网下载:请点击 JDK版本:1.8.0_171 zookeeper-3.4.10 二.kafka安装配置 ...

  10. jdk1&period;8&period;0&lowbar;45源码解读——LinkedList的实现

    jdk1.8.0_45源码解读——LinkedList的实现 一.LinkedList概述 LinkedList是List和Deque接口的双向链表的实现.实现了所有可选列表操作,并允许包括null值 ...