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. INI 文件的读写操作

    在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...

  2. nodejs生成UID(唯一标识符)——node-uuid模块

    unique identifier 惟一标识符        -->> uid 在项目开发中我们常需要给某些数据定义一个唯一标识符,便于寻找,关联. node-uuid模块很好的提供了这个 ...

  3. 8.adr与ldr伪指令的区别

    ldr和adr都是伪指令,区别是ldr是长加载.adr是短加载. 重点:adr指令加载符号地址,加载的是运行时地址: ldr指令加载符号地址时,加载的是链接地址.

  4. javascript的replace+正则 实现ES6的字符串模版

    采用拼接字符串的形式,将 JSON 数据嵌入 HTML 中.开始时代码量较少,暂时还可以接受.但当页面结构复杂起来后,其弱点开始变得无法忍受起来: 书写不连贯.每写一个变量就要断一下,插入一个 + 和 ...

  5. python语句表达式——黑板客老师课程学习

    1.赋值 多重赋值: a,b=1,2 a,b=’beijing’,’sh’ a,b=’bj’ a,b=(1,2) a,b=[1,2] …… 2.输入输出 输入: raw_input()   原始输入 ...

  6. PHP调试总结

    PHP调试总结一,环境方面,比如查看安装扩展是否生效,是总支持某扩展.可以在web目录中建一个phpinfo.php在里面输入<?phpphpinfo();?>在浏览器上访问一下,会输出P ...

  7. WebService 基础使用&cxf第三方Service使用

    1.通过Jax-ws自己发布一个webservice 解析:用webservice发布HelloWorld JAX-WS本质就是通过Socket来实现的.2.WSDL文档描述如何直接变成java代码 ...

  8. Convert HTML Entities

    function convert(str) { // :) //return str; var HTML_Entities = { '&':'&', '<':'<', '& ...

  9. Boo who

    function boo(bool) { // What is the new fad diet for ghost developers? The Boolean. //return bool; r ...

  10. poj 3368 Frequent values(RMQ)

    /************************************************************ 题目: Frequent values(poj 3368) 链接: http ...