UVA11367 Full Tank? 【分层图最短路】
题目
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))\)
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 1005,maxm = 20005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
return out * flag;
}
int n,m,h[maxn],ne = 2;
struct EDGE{int to,nxt,w;}ed[maxm];
void build(int u,int v,int w){
ed[ne] = (EDGE){v,h[u],w}; h[u] = ne++;
ed[ne] = (EDGE){u,h[v],w}; h[v] = ne++;
}
int d[maxn][105],p[maxn],vis[maxn][105];
struct node{int u,c,f;};
inline bool operator <(const node& a,const node& b){return a.f > b.f;}
priority_queue<node> q;
int solve(int S,int T,int C){
for (int i = 1; i <= n; i++)
for (int j = 0; j <= C; j++)
d[i][j] = INF,vis[i][j] = 0;
d[S][0] = 0;
q.push((node){S,0,d[S][0]});
int v; node u;
while (!q.empty()){
u = q.top(); q.pop();
if (vis[u.u][u.c]) continue;
vis[u.u][u.c] = true;
Redge(u.u)
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]){
d[to][v] = d[u.u][u.c]; q.push((node){to,v,d[to][v]});
}
if (!vis[u.u][u.c + 1] && d[u.u][u.c + 1] > d[u.u][u.c] + p[u.u]){
d[u.u][u.c + 1] = d[u.u][u.c] + p[u.u];
q.push((node){u.u,u.c + 1,d[u.u][u.c + 1]});
}
}
int ans = INF;
for (int i = 0; i <= C; i++) ans = min(ans,d[T][i]);
return ans;
}
int main(){
n = read(); m = read();
REP(i,n) p[i] = read();
int a,b,w;
while (m--){
a = read() + 1; b = read() + 1; w = read();
build(a,b,w);
}
int q = read(),S,T,C,ans;
while (q--){
C = read(); S = read() + 1; T = read() + 1;
ans = solve(S,T,C);
if (ans == INF) puts("impossible");
else printf("%d\n",ans);
}
return 0;
}
UVA11367 Full Tank? 【分层图最短路】的更多相关文章
- poj3635Full Tank?[分层图最短路]
Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7248 Accepted: 2338 Descri ...
- HDU 5669 线段树优化建图+分层图最短路
用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...
- BZOJ 2763 分层图最短路
突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...
- 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)
[题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...
- 【网络流24题】 No.14 孤岛营救问题 (分层图最短路)
[题意] 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛, 营救被敌军俘虏的大兵瑞恩. 瑞恩被关押在一个迷宫里, 迷宫地形复杂, 但幸好麦克得到了迷宫的地形图. 迷宫的外形是 ...
- BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路
BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路 Description “我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切, ...
- BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路
BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M ...
- Nowcoder contest 370H Rinne Loves Dynamic Graph【分层图最短路】
<题目链接> 题目大意:Rinne 学到了一个新的奇妙的东西叫做动态图,这里的动态图的定义是边权可以随着操作而变动的图.当我们在这个图上经过一条边的时候,这个图上所有边的边权都会发生变动. ...
- ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】
<题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...
- BZOJ2662[BeiJing wc2012]冻结——分层图最短路
题目描述 “我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„” 在这个愿望被实现以后的世界里,人们享受着魔法卡片(Spe ...
随机推荐
- Netbackup8.0以上版本,服务端生成证书,客户端获取、更新证书方式(整理中)
创建重发令牌 如果非主控主机已在主服务器上注册但其基于主机ID的证书不再有效,则可以重新颁发基于主机ID的证书.例如,证书在过期,被撤销或丢失时无效. 重发令牌是一种可用于重新颁发证书的令牌.它是一种 ...
- mac安装webpack失败
最近开始接触构建工具webpack,公司电脑是 windows,而我自己的呢是mac.本来以为在自己电脑安装很简单,但是出了点问题,所以写出来分享下. 这里用npm的方式安装,首先你要安装node.j ...
- Fruit Ninja(取随机数)
链接:https://www.nowcoder.com/acm/contest/163/A来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524 ...
- openstack rocky kolla docker
https://docs.openstack.org/kolla-ansible/latest/user/quickstart.html
- C#动态数组ArrayList
在C#中,如果需要数组的长度和元素的个数随着程序的运行不断改变,就可以使用ArrayList类,该类是一个可以动态增减成员的数组. 一.ArrayList类的常用属性和方法 1. ArrayList类 ...
- fei33423 工作 职场 格言
对老板: 1. 老板不知道你做的事情(目标设定) 2. 老板要的是规划(对上报告), 自己给自己设定 金字塔四位下的目标,各种维度.如何细化. 2.1 明确老板期望 2.2 与老板达成共识 2.3 ...
- c++调用系统关机命令 c++调用暂停命令
#include<stdlib.h> int main() { //调用系统dos命令 system("shutdown -s -t 120"); ; } system ...
- javascript自定义一个迭代器
js中对象分为可迭代和不可迭代 如果是可迭代哪它就会有一个[Symbol.iterator] 函数 这个函数就是对象的迭代器函数,如用for of 如果遍历的对象没有这个迭代方法那么就会报错 for ...
- paper:基于verilog HDL 的高速可综合FSM设计
1.寄存器输出型状态机 VS 组合逻辑输出型状态机 2.状态编码方法 这块讲的不好,也比较少. 3.系统设计中模块划分的指导性原则
- python3.6:DLL load failed:找不到指定的模块(from PyQt5 import QtCore)
本人小白搭建pyqt环境时遇到问题 运行代码 from PyQt5 import QtCore' 发现错误 ImportError: DLL load failed: 找不到指定的模块 这个问题折磨了 ...