【题解】

用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. mysql-sql语言参考

    字段去重查询 select distinct style from music 批量修改某字段 update music set style = "ost"  where styl ...

  2. js技巧专题篇: 页面跳转

    本篇主要介绍网页上常见的页面跳转技术.页面跳转有几种方式,比较常用的是window.location.href,window.location.replace,window.open,当然还有目前比较 ...

  3. spark-streaming-kafka-0-8 和 0-10的使用区别

    一.spark-streaming-kafka-0-8_2.11-2.0.2.jar 1.pom.xml <!-- https://mvnrepository.com/artifact/org. ...

  4. MyBatis Generator 生成的example 如何使用 and or 简单混合查询

    简单介绍: Criteria,包含一个Cretiron的集合,每一个Criteria对象内包含的Cretiron之间是由AND连接的,是逻辑与的关系. oredCriteria,Example内有一个 ...

  5. Star Schema and Snowflake Schema

    在设计数据仓库模型的时候,最常见的两种是星型模型与雪花模型.选择哪一种需要根据业务需求以及性能的多重考量来定. 星型模型 在星型模型当中,一张事实表被若干张维度表所包围.每一个维度代表了一张表,有主键 ...

  6. python 中的 metaclass

    最遇到一个问题. class Meta(type): pass class M1(Meta): pass class M2(metaclass=M1): pass class Test(M2,meta ...

  7. TableLayoutPanel 行高列宽设置

    /// <summary> /// 获取TableLayoutPanel指定行的高度 /// </summary> /// <param name="layou ...

  8. wcf 数值类型赋值不能的问题解决

    客户端给对象int类型赋值,服务端收到值为0 网上给出的方案 1.数值型字段+isrequired属性.能解决问题,但没有说明原因.数值型默认不赋值,不科学. 2.emitdefaultvalue.没 ...

  9. VS2010 如何自动生成UML图

    项目名---右键----查看类图

  10. AM二次开发中选择指定范围内的对象

    使用Spatial可以快速选择指定范围内的对象 例如下面的代码可以选择所有在[0,0,0]-[10m,10m,10m]这个盒子之内的对象: 其中ElementsInBox还可以指定对象类型做进一步筛选 ...