poj3635 优先队列+打标记+广搜
After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?
To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.
Input
The first line of input gives 1 ≤ n ≤ 1000 and 0 ≤ m ≤ 10000, the number of cities and roads. Then follows a line with n integers 1 ≤ pi ≤ 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 ≤ u, v < n and 1 ≤ d ≤ 100, telling that there is a road between u and v with length d. Then comes a line with the number 1 ≤ q ≤ 100, giving the number of queries, and q lines with three integers 1 ≤ c ≤ 100, s and e, where c is the fuel capacity of the vehicle, s is the starting city, and e is the goal.
Output
For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or "impossible" if there is no way of getting from s to e with the given car.
Sample Input
5 5
10 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4
Sample Output
170
impossible
题意:给出一张图,n<=1000,m<=10000. 有一辆车想从图的一个地方到达另外一个地方,每个点是一个卖油的地方,每个地方买的有价格不一样,车的最大装油量是c,求初始点到终止点的最小花费。 因为状态数只有n*100=10W 所以放心的爆搜就好了 ,,自己太蠢 爆搜都不敢写‘’
#include<cstdio>
#include<cstring>
#include<queue>
const int N=;
using namespace std;
struct Edge{
int v,w,next;
}e[];
int tot,head[N];
void add(int u,int v,int w){
e[tot].w=w;
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
}
int n,m,p[N],x,y,z,dp[N][],c,s,t;
bool vis[N][];
struct Node{
int u,cost,oil;
Node(){}
Node(int a,int b,int c):u(a),cost(b),oil(c){}
bool operator<(const Node &A)const{
return cost>A.cost;
}
};
void bfs(){
memset(dp,0x3f,sizeof(dp));
memset(vis,,sizeof(vis));
dp[s][]=;
priority_queue<Node>Q;
Q.push(Node(s,,));
while(!Q.empty()){
Node now=Q.top();Q.pop();
int o=now.oil,u=now.u,cost=now.cost;
vis[u][o]=;
if(u==t) {
printf("%d\n",cost);
return ;
}
if(o+<=c&&!vis[u][o+]&&dp[u][o]+p[u]<dp[u][o+]) {
dp[u][o+]=dp[u][o]+p[u];
Q.push(Node(u,dp[u][o+],o+));
}
for(int i=head[u];i+;i=e[i].next){
int v=e[i].v,w=e[i].w;
if(o>=w&&!vis[v][o-w]&&cost<dp[v][o-w]){
dp[v][o-w]=cost;
Q.push(Node(v,dp[v][o-w],o-w));
}
}
}
puts("impossible");
}
int main(){
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=;i<n;++i) scanf("%d",p+i);
for(int i=;i<=m;++i) {
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
int T;
for(scanf("%d",&T);T--;){
scanf("%d%d%d",&c,&s,&t);
bfs();
}
}
poj3635 优先队列+打标记+广搜的更多相关文章
- HDU 3152 Obstacle Course(优先队列,广搜)
题目 用优先队列优化普通的广搜就可以过了. #include<stdio.h> #include<string.h> #include<algorithm> usi ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- USACO Milk Routing /// 优先队列广搜
题目大意: 在n个点 m条边的无向图中 需要运送X单位牛奶 每条边有隐患L和容量C 则这条边上花费时间为 L+X/C 求从点1到点n的最小花费 优先队列维护 L+X/C 最小 广搜到点n #inclu ...
- POJ-3635 Full Tank? (记忆化广搜)
Description After going through the receipts from your car trip through Europe this summer, you real ...
- 中南大学oj:1336: Interesting Calculator(广搜经典题目)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...
- hdu5025 状态压缩广搜
题意: 悟空要救唐僧,中途有最多就把钥匙,和最多五条蛇,要求就得唐僧并且拿到所有种类的钥匙(两个1只拿一个就行),拿钥匙i之前必须拿到钥匙i-1,打蛇多花费一秒,问救出唐僧并且拿到所有种类 ...
- VIJOS-P1340 拯救ice-cream(广搜+优先级队列)
题意:从s到m的最短时间.(“o"不能走,‘#’走一个花两个单位时间,‘.'走一个花一个单位时间) 思路:广搜和优先队列. #include <stdio.h> #include ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
随机推荐
- 《Redis设计与实现》之第十二章:事件
Redis服务器是一个事件驱动程序,服务器需要处理两类事件: 文件事件: 文件事件就是服务器对套接字(socket)操作的抽象,服务器和客户端的通信会产生文件事件 时间事件: 时间事件就是服务器对定时 ...
- 算法竞赛进阶指南--在单调递增序列a中查找小于等于x的数中最大的一个(即x或x的前驱)
在单调递增序列a中查找<=x的数中最大的一个(即x或x的前驱) while (l < r) { int mid = (l + r + 1) / 2; if (a[mid] <= x) ...
- HDU - 6187 (最大生成树) 最小生成树
Destroy Walls Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- muduo网络库源码学习————Timestamp.cc
今天开始学习陈硕先生的muduo网络库,moduo网络库得到很多好评,陈硕先生自己也说核心代码不超过5000行,所以我觉得有必要拿过来好好学习下,学习的时候在源码上面添加一些自己的注释,方便日后理解, ...
- 《Docker从入门到跑路》之存储卷介绍
默认情况下,容器会随着用户删除而消失,包括容器里面的数据.如果我们要对容器里面的数据进行长久保存,就不得不引用存储卷的概念. 在容器中管理数据持久化主要有两种方式:1.数据卷(data volumes ...
- Nginx入门资料
最近在学习Nginx,记录一下自己的学习历程. 1. Nginx开发从入门到精通 (淘宝技术团队编写,值得一看) 2. <深入理解Nginx:模块开发与架构解析> 3. Nginx模块开发 ...
- 在windows环境里,用Docker搭建Redis开发环境(新书第一个章节)
大家都知道高并发分布式组件的重要性,而且如果要进大厂,这些技术不可或缺.但这些技术的学习难点在于,大多数项目里的分布式组件,都是搭建在Linux系统上,在自己的windows机器上很难搭建开发环境,如 ...
- 【Hadoop离线基础总结】Sqoop数据迁移
目录 Sqoop介绍 概述 版本 Sqoop安装及使用 Sqoop安装 Sqoop数据导入 导入关系表到Hive已有表中 导入关系表到Hive(自动创建Hive表) 将关系表子集导入到HDFS中 sq ...
- 值得收藏的js原型详解
从虚无到Object 起初,地是空虚混沌,渊面黑暗:这时候一切还是null 神说,要有原型,于是就有了prototype 原型从凭空产生,于是需要一个指向于null的特征,人们把这种特征叫做隐式原型, ...
- 嫌弃Apriori算法太慢?使用FP-growth算法让你的数据挖掘快到飞起
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是机器学习专题的第20篇文章,我们来看看FP-growth算法. 这个算法挺冷门的,至少比Apriori算法冷门.很多数据挖掘的教材还会 ...