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. 0,SFDC 管理员篇 - 整体框架

    SFDC 框架布局, 通过以下分类我们可以了解Salesforce 每个云上所提供的功能和用途. ●销售云 Sales Cloud 销售自动化(Sales ForceAutomation,SFA)&a ...

  2. CGContextRef 绘图

    如何绘制一个矩形 添加一个属性 @property(nonatomic,strong) NSString* RectNumber; 1. 首先重写UIview的drawRect方法 - (void)d ...

  3. Unknow Unknow

    Unknow Unknow,讲的大概意思是:你不知道这事情的时候,那么请不要轻易下任何结论.例如,这个太难了,放弃吧.嗯,这个是捷径.而最后却是,困难的路越走越容易,容易的路越走越难.只是一种常见的现 ...

  4. Ubuntu解压命令大全

    tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gunz ...

  5. APK Downgrade Method working fine on LINE latest version 6.7.1

    Line is one of the most popular messaging Apps, especially in Asia. On March 3 I downgraded the app ...

  6. 【转】详解Oracle的dual表

    网址:http://www.adp-gmbh.ch/ora/misc/dual.html dual is a table which is created by oracle along with t ...

  7. CString转换成char*

    CString转换成char* :charSource = (char*)strSource.GetBuffer(0); 法2:charSource = (char*)strSource.GetBuf ...

  8. Node.js 的初步理解

    Node.js 是一个采用C++语言编写的后端的 Javascript 的运行环境, 它使用了 google 的 V8虚拟机来解释和执行代码.Node.js 的有许多有用的内置的模块,比如 http, ...

  9. 企业好助手U-Mail邮件服务器软件

    对于一家企业,最核心的是什么?有人说人才,有人说技术,也有人说是数据(信息),依小编来 看,这些答案都没错,人才会流动,技术在不断更新,唯有数据(信息),日积月累,将沉淀为公司历史的一部分,作为决策的 ...

  10. 解决:HttpClient导致应用出现过多Close_Wait的问题

    最近发现一个问题,在服务器上通过netstat命令发现有大量的Close_Wait长时间存在,甚至有时候数量接近1000: 查看服务器参数(etc/sysctl.conf): net.ipv4.tcp ...