题目链接:传送门
Time Limit: 1000MS | Memory Limit: 65536K | |
Total 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 ≤ 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
题目大意:一辆车从起点到终点,给定汽车的邮箱容量,给出每个点加每单位油的价格,点与点间的距离,(起点油量为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;
}