poj3635—Full Tank?(spfa+dp)

来源:互联网 发布:埃米纳姆 知乎 编辑:程序博客网 时间:2024/05/22 17:51

题目链接:传送门

Full Tank?
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7521 Accepted: 2438

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


题目大意:一辆车从起点到终点,给定汽车的邮箱容量,给出每个点加每单位油的价格,点与点间的距离,(起点油量为0)计算所需最少的费用。


解题思路:spfa+dp,dp[i][j]记录汽车到达第i个点所剩j单位油所用的费用,维护一个优先队列,将产生的新的状态压入队列,前一个状态向下产生的两个新的状态1.汽车在原结点加一个单位的油2.汽车驶向下一个结点。


#include <cstdio>  #include <cstring>  #include <cmath>  #include <iostream>  #include <queue>#include <set>#include <string>#include <cctype>#include <vector>#include <bitset>using namespace std;  const int size1 = 10100;const int size2 = 1010;struct Node{int node,cost,oil;Node( int n , int c , int o ){ node = n ; cost = c ; oil = o; }bool operator < ( const Node& a ) const{return cost>a.cost;}};int cap[size2],dp[size2][105],vis[size2][105];int c,s,e;struct Edge{int node,Len;Edge*next;}m_edge[size1*2];Edge*head[size2];int Ecnt;void mkEdge( int u , int v , int w ){m_edge[Ecnt].node = u;m_edge[Ecnt].Len = w;m_edge[Ecnt].next = head[v];head[v] = m_edge+Ecnt++;m_edge[Ecnt].node = v;m_edge[Ecnt].Len = w;m_edge[Ecnt].next = head[u];head[u] = m_edge+Ecnt++;}void initData(){Ecnt = 0;fill( head , head+size2 , (Edge*)0 );}void bfs(){memset( vis , 0 , sizeof(vis) );memset( dp , 1 , sizeof(dp) );priority_queue<Node>p;p.push(Node(s,0,0));dp[s][0] = 0;while( !p.empty() ){Node q = p.top();p.pop();int n = q.node,cost = q.cost,o = q.oil;vis[n][o] = 1;if( n == e ){printf("%d\n",cost);return;}//加一个单位的油if( o+1<=c && !vis[n][o+1] && dp[n][o]+cap[n]<dp[n][o+1] ){dp[n][o+1] = dp[n][o]+cap[n];p.push(Node(n,dp[n][o+1],o+1));}//走到下一个结点for( Edge*k = head[n] ; k ; k = k->next ){int L = k->Len,N = k->node;if( o>=L && !vis[N][o-L] && cost<dp[N][o-L] ){dp[N][o-L] = cost;p.push(Node(N,dp[N][o-L],o-L));}}}printf("impossible\n");}int main(){int n,m,u,v,w,q;scanf("%d%d",&n,&m);initData();for( int i = 0 ; i < n ; ++i ){scanf("%d",&cap[i]);}for( int i = 0 ; i < m ; ++i ){scanf("%d%d%d",&u,&v,&w);mkEdge( u , v , w );}scanf("%d",&q);for( int i = 0 ; i < q ; ++i ){scanf("%d%d%d",&c,&s,&e);bfs();}return 0;}


0 0
原创粉丝点击