poj 3635/hdu 1676 Full Tank? 车辆加油+最短路
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? 车辆加油+最短路的更多相关文章
- POJ 3635 Full Tank? 【分层图/最短路dp】
任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- 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 ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- Full Tank? POJ - 3635 (bfs | 最短路)
After going through the receipts from your car trip through Europe this summer, you realised that th ...
- 【POJ 3635】 Full Tank
[题目链接] http://poj.org/problem?id=3635 [算法] 优先队列BFS 实现类似于堆优化dijkstra [代码] #include <algorithm> ...
随机推荐
- bash操作小结
刚开始学写bash脚本,发现有很多需要注意的细节问题,在这里记录一下便于记忆: 1. help test 帮助 2. bash提供的数组数据结构,它是以数字为下标的,和C语言从0开始的下一样 参考 ...
- linux文件上传
租了个服务器,要把自己打的war包打上去发布,服务器是linux的(本机是mac).以前都是用wget命令,把需要上传的东西先放到github上面再在服务器上用wget命令下载.最近不知道什么原因,w ...
- 给JAVA初学者的50个忠告
摘自: http://wenku.baidu.com/link?url=p5vDKt5bKzUXxG_hAsB9XopHJRROeovOGGh4jnXGZXiUedR-fNOLO7zb6ddENo5U ...
- Android开发--环境的配置
一 Android开发环境:JDK.eclipse ADT.海马模拟器或者夜神模拟器.配置之前先保证运行内存足够大,不然会导致运行卡. 二 JDK(不用安装) 1.jdk官方下载地址:http://w ...
- js图片变换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Spark 集群安装
一.安装jdk 二.安装scala 三.安装Spark 1.解压 tar -zxvf spark-1.5.1-bin-hadoop2.6.tgz 2.cp spark-env.sh.template ...
- 使用css3中calc()进行自适应布局
calc()能做什么? calc()可以通过计算得到元素的宽度或者高度,让我们很容易进行自适应布局. 你可以为一个div元素,使用百分比.em.px和rem单位值计算出其宽度或者高度,比如说“widt ...
- 循序渐进Python3(三) -- 3 -- 内置函数
上一篇我们又介绍了26个Python内置函数.现回忆一下吧: 1.all 2.any 3.ascii 4.bin 5.bool 6.bytes 7.bytearray 8.callable 9.chr ...
- <a>标签的四种状态
a:link a:visited a:hover a:active
- java的四种取整方法
java 中取整操作提供了四种方法:分别是: public static double ceil(double a)//向上取整 public static double floor(double ...