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 ...
随机推荐
- Vue Cli 3 打包上线 部署到Apache Tomcat服务器
使用 npm run build 打包项目 在根目录中有一个dist文件夹 我使用的服务器是 Apache Tomcat 把项目放进tomcat /webapps 中 启动服务器 <mac O ...
- 使用 html5 FileReader 获取图片, 并异步上传到服务器 (不使用 iframe)
为什么80%的码农都做不了架构师?>>> 原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码post到服务器. 3.根据 ...
- Docker学习之搭建MySql容器服务
描述 MySQL 5.6 SQL数据库服务器Docker镜像,此容器映像包含用于OpenShift的MySQL 5.6 SQL数据库服务器和一般用法.用户可以选择RHEL和基于CentOS的图像.然后 ...
- 通过fiddler抓取IDEA的请求
2019独角兽企业重金招聘Python工程师标准>>> 因为fiddler默认是代理的8888端口,所以设置一下IDEA的请求使用本地的8888作为代理发出. 1."EDI ...
- JVM调优方法笔记
1.性能工具介绍 jvisualvm jmap jstat jstack/threaddump jprofiler jmeter 2.性能调优4步骤 重现问题 定位问题 模拟问题 解决问题 http: ...
- 自动安装带nginx_upstream_check_module模块的Nginx脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #!/bin/bash useradd -s /sbin/no ...
- 地表最简单安装MySQL及配置的方法,没有之一
第一步下载我的压缩包 链接:https://pan.baidu.com/s/1EE40dU0j2U1d-bAfj7TeVA 提取码:n25c 复制这段内容后打开百度网盘手机App,操作更方便哦 第二步 ...
- LeetCode 56,57,60,连刷三题不费劲
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第34篇文章,刚好接下来的题目比较简单,很多和之前的做法类似.所以我们今天出一个合集,一口气做完接下来的57.5 ...
- [NOI 2020 Online] 入门组T1 文具采购(洛谷 P6188)题解
原题传送门 题目部分:(来自于考试题面,经整理) [题目描述] 小明的班上共有 n 元班费,同学们准备使用班费集体购买 3 种物品: 1.圆规,每个 7 元. 2.笔,每支 4 元. 3.笔记本,每本 ...
- springboot后端校验
这一篇讲解了如何定义特殊的校验 https://www.cnblogs.com/cjsblog/p/8946768.html https://blog.csdn.net/xgblog/article/ ...