博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3635Full Tank?[分层图最短路]
阅读量:6860 次
发布时间:2019-06-26

本文共 2884 字,大约阅读时间需要 9 分钟。

Full Tank?
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7248   Accepted: 2338

Description

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 ≤ uv < 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 510 10 20 12 130 1 90 2 81 2 11 3 112 3 7210 0 320 1 4

Sample Output

170impossible

Source


题意:有了油量限制,每个点加油价格不一样,对于每个询问求最少花费

一看到d[i][j]这样的状态表示,马上想到分层图最短路,有点类似DP的思想
按油量分层,d[i][j]表示到节点i还有j个油的最小花费(不是最短路)
两种决策,加一个油或者直接走
感觉用Dijkstra写比较好
注意状态判重,多判一次也无所谓了
 
小优化:离线处理询问,相同起点同时处理
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;const int N=1e3+5,M=1e4+5,V=105;int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){ if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f;}struct edge{ int v,ne,w;}e[M<<1];int h[N],cnt=0;void ins(int u,int v,int w){ cnt++; e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;}int n,m,Q,u,v,w,p[N],c,st,ed;struct hn{ int u,d,f; bool operator <(const hn &rhs)const{ return d>rhs.d;} hn(int a=0,int b=0,int c=0):u(a),d(b),f(c){}};int d[N][V];bool done[N][V];void bfs(int c,int st,int ed){ memset(d,127,sizeof(d)); memset(done,0,sizeof(done)); priority_queue
q; q.push(hn(st,0,0)); d[st][0]=0; while(!q.empty()){ hn x=q.top();q.pop(); int u=x.u,f=x.f,cost=x.d;//printf("bfs %d %d\n",u,f); //if(done[u][f]) continue; done[u][f]=1; if(u==ed){printf("%d\n",cost);return;} if(f+1<=c&&!done[u][f+1]&&d[u][f]+p[u]
=w&&!done[v][f-w]&&d[v][f-w]>cost){ d[v][f-w]=cost; q.push(hn(v,cost,f-w)); } } } printf("impossible\n");}int main(){ n=read();m=read(); for(int i=0;i

 

 

转载地址:http://dfxyl.baihongyu.com/

你可能感兴趣的文章
浏览器根对象window之performance
查看>>
让div 充满整个body
查看>>
常用排序算法
查看>>
程序员保持快乐活跃的6个好习惯(转)
查看>>
找工作的一些感悟——前端小菜的成长
查看>>
jSON Call can throw but it is not marked with try
查看>>
基于bootstrap的jQuery多级列表树插件 treeview
查看>>
node06
查看>>
笔试题[转]
查看>>
图片轮换
查看>>
PHP数据结构练习笔记--栈
查看>>
JSON对象配合jquery.tmpl.min.js插件,手动攒出一个table
查看>>
编译安装QEMU 及卸载
查看>>
关于php-fpm与nginx进程重载的坑
查看>>
P2S、P2P、P2SP之对比
查看>>
笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用...
查看>>
替代变量
查看>>
73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】
查看>>
UNIX环境高级编程——pthread_create的问题
查看>>
接口继承中一个常见问题的思考
查看>>