普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解。

优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解。

且对于优先队列BFS来说,每次存入队列的不光是像普通BFS的状态,还有当前状态对应的代价,并且是依据最小代价进行扩展。每次状态被更近之后,将其入队。

对于本题来说

状态选取:当前所在城市,当前油量

代价函数:当前状态所对应的最小花费

代码如下:

#include <cstdio>
#include <iostream>
#include <utility>
#include <algorithm>
#include <queue>
#include <memory.h>
#define cls(a,b) memset(a,b,sizeof(a))
using namespace std; inline int read(){
int x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
} int d[1001][101],cost[1001];
bool vis[1001][101];
int n,m,q;
int c,st,ed;
struct edge{
int to,w;
edge(int to=0,int w=0):to(to),w(w){}
};
vector<edge> G[1001];
inline void add_edge(int from,int to,int w){
G[from].push_back(edge(to,w));
G[to].push_back(edge(from,w));
} int bfs(){
priority_queue< pair<int,pair<int,int> > > q;//存代价和状态
cls(vis,0);cls(d,0x3f);
d[st][0]=0;q.push(make_pair(0,make_pair(st,0)));
while(q.size()){
int city=q.top().second.first,fuel=q.top().second.second;q.pop();
if(vis[city][fuel])continue;//每个状态只会被扩展一次 if(city==ed)return d[city][fuel];//每个状态出队扩展时,对应最优解 vis[city][fuel]=1;//记录状态是否被扩展 if(fuel<c&&d[city][fuel+1]>d[city][fuel]+cost[city]){//满足条件,并且可以更新最优解
d[city][fuel+1]=d[city][fuel]+cost[city];
q.push(make_pair(-d[city][fuel+1],make_pair(city,fuel+1)));
}
for(int i=0;i<G[city].size();i++){
int to=G[city][i].to,w=G[city][i].w;
if(fuel>=w&&d[to][fuel-w]>d[city][fuel]){
d[to][fuel-w]=d[city][fuel];
q.push(make_pair(-d[to][fuel-w],make_pair(to,fuel-w)));//负号为改大根堆为小根堆
}
}
}
return -1;
} void read_and_parse(){
n=read(),m=read();
for(int i=1;i<=n;i++)cost[i]=read();
for(int i=1;i<=m;i++){
int from=read()+1,to=read()+1,w=read();
add_edge(from,to,w);
}
} void solve(){
q=read();
while(q--){
c=read(),st=read()+1,ed=read()+1;
int ans=bfs();
if(ans==-1)puts("impossible");
else printf("%d\n",ans);
}
} int main(){
read_and_parse();
solve();
return 0;
}

【POJ3635】Full Tank 优先队列BFS的更多相关文章

  1. POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?

    然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...

  2. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  3. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

  5. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  6. poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。

    用的BFS+优先队列+二进制压缩状态判重+链式前向星, TLE,好像有人这样过了...好像要用A*算法,还不太会,所以暂时放弃.但是也学会了很多,学习了链式前向星,更深理解了BFS求最优的时候,什么时 ...

  7. Battle City 优先队列+bfs

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  8. 【UESTC 482】Charitable Exchange(优先队列+bfs)

    给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...

  9. cdoj 482 优先队列+bfs

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

随机推荐

  1. Mvc_缓存浅谈

    缓存是将信息放在内存中以避免频繁访问数据库从数据库中提取数据,在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法. 对于MVC有Control缓存和Action缓存. 一.Control缓存 ...

  2. 【2016.3.22】作业 Word count 小程序

    今天更下word count程序的设计思路及实现方法. 我的程序贴在coding里,这里就先不贴出来了, 我的coding地址:https://coding.net/u/holy_angel/p/wo ...

  3. Week3 关于“微软必应词典客户端”的案例分析

    第一部分  调研,评测 一.iphone客户端的bug挖掘: 1.在例句中点击单词或短语,如果这个时候点得稍微快了一点,关联相应的翻译时会出现混乱. 经过调查发现,这个bug应该是必应得一个全平台错误 ...

  4. BugPhobia终章篇章:学霸在线系统Beta阶段展示

    0x00 :序言 1 universe, 9 planets, 204 countries,809 islands, 7 seas, and i had the privilege to meet y ...

  5. SQL大杂烩

    DML 语句(数据操作语言)Insert.Update. Delete.Merge DDL 语句(数据定义语言)Create.Alter. Drop.Truncate DCL 语句(数据控制语言)Gr ...

  6. PAT 1028 人口普查

    https://pintia.cn/problem-sets/994805260223102976/problems/994805293282607104 某城镇进行人口普查,得到了全体居民的生日.现 ...

  7. Kitematic - VirtualBox is not installed. Docker for windows 10

    Kitematic - VirtualBox is not installed. Docker for windows 10 https://github.com/docker/kitematic/i ...

  8. Building Java Projects with Gradle

    https://spring.io/guides/gs/gradle/ Gradle学习系列教程 https://blog.csdn.net/column/details/gradle-transla ...

  9. XShell+Xmanager实现在XShell中显示远程服务器的图形界面

    http://blog.csdn.net/linghao00/article/details/8768435

  10. Win10 1803 Spring Creators update Consumer edition的版本记录

    安装时可选择的版本列表 安装完之后的版本: 3. 时间线更新 4. Focus assistant