http://acm.hdu.edu.cn/showproblem.php?pid=1676

给出一张图,n<=1000,m<=10000. 有一辆车想从图的一个地方到达另外一个地方,每个点是一个卖油的地方,每个地方买的有价格不一样,车的最大装油量是c<=100,求初始点到终止点的最小花费。



可以 dp[i][j] 表示走到i点剩余j个单位的汽油时的最小花费,难点在于车需要加油,加多少油,转换问题后发现还是简单的bfs

就是维护一个费用优先队列,每到达一个节点都有两种可扩展的状态,一是加一个单位的油,二是走向邻接点,然后不断的将状态加入优先队列中

适当剪枝即可,vis控制一种状态只更新一次

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 0x7fffffff;
const int maxn = 1005,maxm = 10005;
struct edge{
int v,w,next;
edge(){};
edge(int vv,int ww,int nnext):v(vv),w(ww),next(nnext){};
}e[maxm<<1];
int head[maxn],p[maxn],ecnt,dp[maxn][105];//i城市j
bool vis[maxn][105];
int n,m,c,st,en;
void add(int u,int v,int w)
{
e[ecnt] = edge(v,w,head[u]);
head[u] = ecnt++;
e[ecnt] = edge(u,w,head[v]);
head[v] = ecnt++;
}
void init()
{
clr1(head),ecnt = 0;
}
struct node{
int u,cost,last;
node(){};
node(int uu,int cc,int tt):u(uu),cost(cc),last(tt){};
bool operator < (const node &a)const{
return a.cost < cost;
}
};
void bfs()
{
for(int i = 0;i < n;++i)
for(int j = 0;j <= 100;++j)
dp[i][j] = inf;
clr0(vis);
priority_queue<node> q;
q.push(node(st,0,0));
dp[st][0] = 0;
while(!q.empty()){
node cur = q.top();q.pop();
int u = cur.u,cost = cur.cost,last = cur.last;
if(u == en){
printf("%d\n",cost);
return ;
}
vis[u][last] = 1;
if(last + 1 <= c && !vis[u][last + 1] && dp[u][last + 1] > dp[u][last]+p[u]){
dp[u][last+1] = dp[u][last] + p[u];
q.push(node(u,dp[u][last+1],last+1));
}
for(int i = head[u];i != -1;i = e[i].next){
int v = e[i].v,w = e[i].w;
if(last >= w && !vis[v][last - w] && dp[v][last - w] > cost){
dp[v][last - w] = cost;
q.push(node(v,cost,last - w));
}
}
}
puts("impossible");
}
int main()
{
init();
RD2(n,m);
for(int i = 0;i < n;++i)
RD(p[i]);
int u,v,w,q;
while(m--){
RD3(u,v,w);
add(u,v,w);
}
RD(q);
while(q--){
RD3(c,st,en);
bfs();
}
return 0;
}

poj 3635/hdu 1676 Full Tank? 车辆加油+最短路的更多相关文章

  1. POJ 3635 Full Tank? 【分层图/最短路dp】

    任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  2. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  3. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  4. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  5. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  6. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  7. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  8. Full Tank? POJ - 3635 (bfs | 最短路)

    After going through the receipts from your car trip through Europe this summer, you realised that th ...

  9. 【POJ 3635】 Full Tank

    [题目链接] http://poj.org/problem?id=3635 [算法] 优先队列BFS 实现类似于堆优化dijkstra [代码] #include <algorithm> ...

随机推荐

  1. iOS开发--应用设置及用户默认设置【2、读取应用中的设置】

            在上一节中,我们通过探讨应用的系统设置的基本功能,了解运用bundle捆绑包以及plist文件的基本开发.用户能够使用设置应用来声明他们的偏好设置,那么我们怎样去调用用户所设置的参数呢 ...

  2. 解决hibernate只能插入一条数据的问题

    hibernate初学,根据视频教程写好代码后,发现无论执行多少次main函数,数据库中只有一条数据,尝试多次,后来终于发现问题... 使用的工具是:MYSQL 5.7.13   eclipse 4. ...

  3. SQL笔记-第七章,表连接

    SQL中使用JOIN 关键字来使用表连接.表连接有多种不同的类型,被主流数据库系统支持的有交叉连接(CROSS JOIN).内连接(INNER JOIN).外连接(OUTTER JOIN),另外在有的 ...

  4. java学习第5天

    一维数组完了就是二维数组,和一位数组类似 .我们定义二维数组用的是 int[] []arr=new int[m][n],与一维类似,它在堆内存中存放,并以地址的形式访问,如下图..   而遍历二维数组 ...

  5. 去掉hive字段中的tab

    去除空格用trim 去除tab用如下方法 select regexp_replace(secdomainname,'\\s+','') from dwb_cndns_node_secdomain_d ...

  6. 学习打造自己的DEBUG_NEW

    学习范例http://www.cppblog.com/Robertxiao/archive/2012/11/05/194547.html 在使用MFC库开发程序时,我非常喜欢MFC框架中的内存泄漏诊断 ...

  7. VMware学习笔记(一)

    vmware核心产品是vSphere,而vSphere主要包括ESXi和vCenterServer. ESXi不依赖其它操作系统OS,安装在每一台物理机上,ESXi是免费的.在ESXi主机上再安装vS ...

  8. JavaScript:JSON

      JSON是一种数据格式,它并不从属于JavaScript,很多语言都有JSON的解析器和序列化器. 语法 JSON可以表示三种类型: 简单值:使用与JavaScript相同的语法,可以在JSON中 ...

  9. 第九十七天请假 PHP TP框架 MVC模式

    MVC : M->Model 模型(数据层)     V->View 视图(视图层)  C->Controller 控制器(逻辑层)            M : 操作数据(连接数据 ...

  10. asp.net前台绑定时间格式时,定义时间格式

    <%#Eval("news_time","{0:yyyy-MM-dd}") %><%#((DateTime)Eval("news_t ...