POJ3635 Full Tank?
【题解】
用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?的更多相关文章
- POJ-3635 Full Tank? (记忆化广搜)
Description After going through the receipts from your car trip through Europe this summer, you real ...
- POJ3635 Full Tank?(DP + Dijkstra)
题目大概说,一辆带有一个容量有限的油箱的车子在一张图上行驶,每行驶一单位长度消耗一单位油,图上的每个点都可以加油,不过都有各自的单位费用,问从起点驾驶到终点的最少花费是多少? 这题自然想到图上DP,通 ...
- POJ3635 Full Tank?【Dijkstra+DP】
题意: n个城市之间有m条双向路.每条路要耗费一定的油量.每个城市的油价是固定并且已经给出的.有q个询问,表示从城市s走到e,油箱的容量为c,求最便宜的方案. 思路: 用Dijkstra+Heap即可 ...
- POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?
然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...
- poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。
用的BFS+优先队列+二进制压缩状态判重+链式前向星, TLE,好像有人这样过了...好像要用A*算法,还不太会,所以暂时放弃.但是也学会了很多,学习了链式前向星,更深理解了BFS求最优的时候,什么时 ...
- poj练习题的方法
poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- [ASE]项目介绍及项目跟进——TANK BATTLE·INFINITE
童年的记忆,大概是每周末和小伙伴们围坐在电视机前,在20来寸的电视机屏幕里守卫着这个至今都不知道是什么的白色大鸟. 当年被打爆的坦克数量估计也能绕地球个三两圈了吧. 十几年过去了,游戏从2D-3D,画 ...
- poj3635Full Tank?[分层图最短路]
Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7248 Accepted: 2338 Descri ...
随机推荐
- Gravitee.io alert 引擎架构
alert 在我们的实际开发中应用的场景很多,我们需要进行系统状态的查看,以及特殊异常请求的处理 参考架构图 从下图可以看出,还是很方便的,同时支持slack email... 的实时消息通知,而且我 ...
- uname command
The command uname helps us in development special in scripts, see help of the uname uname --help Usa ...
- jsonDB使用手冊
已在github上建立项目:https://github.com/ThinkerCodeChina/jsonDB 博客:http://blog.csdn.net/thinkercode/ 简单介绍: ...
- try catch 用法实例
- 基于.NET平台常用的框架整理 【转载】
[转载] http://www.cnblogs.com/hgmyz/p/5313983.html 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个 ...
- [转]MyBatis中resultType与resultMap区别
MyBatis中关于resultType和resultMap的具体区别如下: MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap.resu ...
- opengl 几何着色器
绘制4条线段 #define GLEW_STATIC #include <GL/glew.h> #include <GLFW/glfw3.h> #include "S ...
- 放一个Dynamicinputs corresponding to Dynamicknobs的Node源码
static const char* const CLASS = "AddInputsSol"; static const char* const HELP = "Add ...
- 开始使用GoJS
GoJS是一个用于实现交互式图表的JavaScript库.本页将向您展示使用GoJS的必要条件. 由于GoJS是一个依赖于HTML5功能的JavaScript库,因此您需要确保您的页面声明它是一个HT ...
- Elasticsearch 5.2.x 使用 Head 插件连接不上集群
如果访问elasticsearch出现跨域的问题,如下: 修改elasticsearch.yml文件 vim $ES_HOME$/config/elasticsearch.yml # 增加如下字段 h ...