【题解】

用dijkstra算法求最短路。同时考虑在每个节点加油(一单位)与否。dp[i][j]记录汽车到达第i个点所剩j单位油所用的费用。

【代码】

 #include <iostream>
#include <map>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
#define maxn 1005
#define maxm 10005 /*
* dp[i][j] denodes the least cost on the ith node
* with j oil left.
*/
int price[maxn], dp[maxn][], vis[maxn][];
int Ecnt, capacaity, sp, ep; struct Node
{
int node, cost, oil;
Node(int n, int c, int o) :
node(n), cost(c), oil(o) {}
bool operator < (const Node &N) const{
return cost > N.cost;
}
}; struct Edge
{
int node;
int len;
Edge* next;
}edges[*maxm]; Edge* head[maxn]; void init()
{
Ecnt = ;
fill(head, head + maxn, (Edge*));
} void build(int u, int v, int w)
{
edges[Ecnt].node = u;
edges[Ecnt].len = w;
edges[Ecnt].next = head[v];
head[v] = edges + Ecnt++; edges[Ecnt].node = v;
edges[Ecnt].len = w;
edges[Ecnt].next = head[u];
head[u] = edges + Ecnt++;
} void dijkstra()
{
memset(vis, , sizeof(vis));
memset(dp, , sizeof(dp));
priority_queue<Node> pq;
pq.push(Node(sp, , ));
dp[sp][] = ;
while (!pq.empty()) {
Node np = pq.top();
pq.pop();
int n = np.node, c = np.cost, o = np.oil;
vis[n][o] = ;
if (n == ep) {
printf("%d\n", c);
return;
}
/* decide to add oil or not */
if (o + <= capacaity && !vis[n][o + ] && dp[n][o] + price[n] < dp[n][o + ]) {
dp[n][o + ] = dp[n][o] + price[n];
pq.push(Node(n, dp[n][o + ], o + ));
}
/* drive to the next node */
for (Edge *Ep = head[n]; Ep; Ep = Ep->next) {
int N = Ep->node, Len = Ep->len;
if (o >= Len && !vis[N][o - Len] && c < dp[N][o - Len]) {
dp[N][o - Len] = c;
pq.push(Node(N, dp[N][o - Len], o - Len));
}
}
}
printf("impossible\n");
return;
} int main()
{
int city_n, road_m, queries;
cin >> city_n >> road_m;
init();
for (int i = ; i < city_n; i++)
cin >> price[i];
for (int i = ; i < road_m; i++) {
int u, v, d;
cin >> u >> v >> d;
build(u, v, d);
}
cin >> queries;
for (int i = ; i < queries; i++) {
cin >> capacaity >> sp >> ep;
dijkstra();
}
//system("pause");
return ;
}

POJ3635 Full Tank?的更多相关文章

  1. POJ-3635 Full Tank? (记忆化广搜)

    Description After going through the receipts from your car trip through Europe this summer, you real ...

  2. POJ3635 Full Tank?(DP + Dijkstra)

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

  3. POJ3635 Full Tank?【Dijkstra+DP】

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

  4. POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?

    然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...

  5. poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。

    用的BFS+优先队列+二进制压缩状态判重+链式前向星, TLE,好像有人这样过了...好像要用A*算法,还不太会,所以暂时放弃.但是也学会了很多,学习了链式前向星,更深理解了BFS求最优的时候,什么时 ...

  6. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  7. 【POJ3635】Full Tank 优先队列BFS

    普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...

  8. [ASE]项目介绍及项目跟进——TANK BATTLE·INFINITE

    童年的记忆,大概是每周末和小伙伴们围坐在电视机前,在20来寸的电视机屏幕里守卫着这个至今都不知道是什么的白色大鸟. 当年被打爆的坦克数量估计也能绕地球个三两圈了吧. 十几年过去了,游戏从2D-3D,画 ...

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

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

随机推荐

  1. Tanks!Tutorial 学习

    using UnityEngine; namespace Complete { public class CameraControl : MonoBehaviour { /// <summary ...

  2. Laya播放unity特效

    杭州-fun 2017/12/5 20:47:12  其实网上就有你搜下就有了现成的脚本,设置帧数和截取时间它会截屏并保存成贴图导入laya生成atlas就能用了   就是unity的截屏功能   就 ...

  3. Python 给实例或者类动态绑定属性和方法

    首页定义一个class,创建一个实例之后,我们可以给该实例绑定任何属性和方法,先定义class: class Student: def __init__(self, name, score): sel ...

  4. webpack 提取 manifest 文件

    当 webpack 生成 bundle 时, 它同时维护一个 manifest 文件.你可以在生成的 vendor bundle 中找到它.manifest 文件描述了哪些文件需要 webpack 加 ...

  5. a标签打开设置

    <a href="http://www.baidu.com" target="_Blank">百度</a> _Blank是新窗口_Sel ...

  6. MySQL通过Navicat实现远程连接

    直接使用Navicat通过IP连接会报各种错误,例如:Error 1130: Host '192.168.1.80' is not allowed to connect to this MySQL s ...

  7. ulimit设置内存限制是否有效

    如何使用ulimit限制物理内存 限制物理内存 $ ulimit -m 512000 经测试,无效. 限制物理内存不起作用,ulimit不支持限制物理内存,可见man ulimit手册 -m The ...

  8. Qt中的标准对话框之QMessageBox

    1. Qt标准对话框 Qt为开发者提供了一些可复用的对话框类型 Qt提供的可复用对话框全部继承自QDialog类 Qt中的对话框的使用方式和QDialog完全一致 2. 标准对话框的使用步骤 ①定义对 ...

  9. Application Request Route实现IIS Server Farms集群负载

    首先你装一个web 平台安装程序:https://www.microsoft.com/zh-CN/download/details.aspx?id=6164 安装完之后会出现打开界面,iis中也可找到 ...

  10. class path resource [spring/ApplicationContext-springmvc.xml] cannot be opened because it does not exist

    配置如下: <init-param>   <param-name>contextConfigLocation</param-name>   <param-va ...