题意:n个城市有m条道路。每个城市的油价不一样,给出起点s和终点t,以及汽车的油箱的容量,求从城市s到城市 t 的最便宜路径。

析:dp[u][i] 表示在第 u 个城市,还剩下 i L升油,一开始用BFS,TLE,要注意效率,用dijkstra,找到城市 t 就该结束了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int pri[maxn];
struct Edge{
int to, val, next;
};
Edge edges[maxn*10<<1];
int head[maxn], cnt; void addEdge(int u, int v, int c){
edges[cnt].to = v;
edges[cnt].val = c;
edges[cnt].next = head[u];
head[u] = cnt++;
} int dp[maxn][105]; struct HeapNode{
int cost, u, last;
bool operator < (const HeapNode &p) const{
return cost > p.cost;
}
}; int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < n; ++i) scanf("%d", pri + i);
ms(head, -1); cnt = 0;
while(m--){
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
addEdge(u, v, c);
addEdge(v, u, c);
}
scanf("%d", &m);
while(m--){
int s, t, c;
scanf("%d %d %d", &c, &s, &t);
for(int i = 0; i < n; ++i) ms(dp[i], INF);
dp[s][0] = 0;
priority_queue<HeapNode> pq;
pq.push((HeapNode){0, s, 0});
bool ok = false;
while(!pq.empty()){
HeapNode h = pq.top(); pq.pop();
if(h.u == t){ printf("%d\n", h.cost); ok = true; break; }
int u = h.u, last = h.last, cost = h.cost;
if(last < c && dp[u][last+1] > cost + pri[u]){
dp[u][last+1] = cost + pri[u];
pq.push((HeapNode){cost + pri[u], u, last + 1});
}
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to;
if(last >= edges[i].val && dp[v][last-edges[i].val] > cost){
dp[v][last-edges[i].val] = cost;
pq.push((HeapNode){cost, v, last-edges[i].val});
}
}
}
if(!ok) puts("impossible");
}
}
return 0;
}

  

UVa 11367 Full Tank? (DP + Dijkstra)的更多相关文章

  1. UVA 11367 - Full Tank? dijkstra+DP

    传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  2. UVA 11367 Full Tank?(bfs最短路)

    n个点m条无向边的图,油箱有上限,每个单位的汽油能走1单位距离,每个城市的油价val[i], 对于每个query,求s到e的最小花费. dp[i][j]表示到达第i个城市,油箱剩余油量j时的最小花费. ...

  3. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  4. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

  5. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  6. uva 10817(数位dp)

    uva 10817(数位dp) 某校有m个教师和n个求职者,需讲授s个课程(1<=s<=8, 1<=m<=20, 1<=n<=100).已知每人的工资c(10000 ...

  7. uva 11367 (Dijkstra+DP)

    题意:一辆汽车在一张无向图中开告诉你每个城市加油的费用.每次给q个查询(起点,终点,油箱容量)问你最小花费是多少. 思路:一道Dijkstra状态的题目.在这种最短路问题中一维的dis数组记录的信息往 ...

  8. POJ3635 Full Tank?(DP + Dijkstra)

    题目大概说,一辆带有一个容量有限的油箱的车子在一张图上行驶,每行驶一单位长度消耗一单位油,图上的每个点都可以加油,不过都有各自的单位费用,问从起点驾驶到终点的最少花费是多少? 这题自然想到图上DP,通 ...

  9. POJ3635 Full Tank?【Dijkstra+DP】

    题意: n个城市之间有m条双向路.每条路要耗费一定的油量.每个城市的油价是固定并且已经给出的.有q个询问,表示从城市s走到e,油箱的容量为c,求最便宜的方案. 思路: 用Dijkstra+Heap即可 ...

随机推荐

  1. UITableView cell 半透明效果,改变cell高度时背景不闪的解决方法

    如果直接指定cell.backgroundColor = = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 ...

  2. python远程调试及celery调试

    部分来自 from: https://www.xncoding.com/2016/05/26/python/pycharm-remote.html 你是否经常要在Windows 7或MAC OS X上 ...

  3. UNION会自动删除重复项,union与union all的差异

    UNION会自动删除重复项,刷选出部分字段的时候要注意了,如果刷选出来的两个字段的值完全相同就会剩下一个. UNION ALL 和 UNION 不同之处在于 UNION ALL 会将左右两个查询的结果 ...

  4. How To Pronounce 3-Syllable Phrases

    How To Pronounce 3-Syllable Phrases Share Tweet Share Tagged With: 3-Syllable Learn about the stress ...

  5. Packed with amazing data about the world in 201

    Only those who have the patience to do simple things,perfectly ever acquire the skill to do difficul ...

  6. mysql 存储过程分页 转载

    /* --名称:MYSQL版查询分页存储过程 by peace 2013-8-14 --输入参数:@fields -- 要查询的字段用逗号隔开 --输入参数:@tables -- 要查询的表 --输入 ...

  7. IOS6新特性之下拉刷新<UIRefreshControl>

    在IOS6未发布之前,几乎都是使用那个UIRefresh在实现下拉刷新,甚至有人还是先了上拉的功能,不得不说牛人很多啊.可能是Apple意识到了这个功能的实用性,在IOS6中增加了下拉刷新,但是上啦还 ...

  8. sam/bam格式

    1)Sam (Sequence Alignment/Map) ------------------------------------------------- 1) SAM 文件产生背景 随着Ill ...

  9. eclipse UTF-8

    1. 你本地开发环境IDE,默认配置也是gbk,改为utf82. 检查你tomcat等服务器中间件GBK改成UTF8eclipse工作空间的编码设置成UTF-8,具体操作是:windows---pre ...

  10. jQuery源码解读一

    (function(window,undefined){...})(window); 这是一个典型的自执行的匿名函数. 为什么会有一个名为undefined的形参呢? undefined不是常量,可以 ...