hdu 1676(二维Dijkstra)

本文介绍了一种寻找两个城市间最低成本路径的算法,考虑到不同城市的燃油价格差异,通过使用带有限制条件的广度优先搜索来解决此问题。该算法考虑了车辆油箱的容量限制,并在状态转移过程中同时考虑了加油和行驶两种操作。
部署运行你感兴趣的模型镜像

Full Tank?

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 142    Accepted Submission(s): 64


Problem 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
 

Source
 

Recommend
lcy
 
n个城市,m条路,一个车有个有容量上限的油箱,各个城市加油的费用不同,现在求起点到终点的最小费用。因为各个城市加油的费用不同,所以简单的走最短路不一定是最优的。我们需要重新设计状态,可以想到到每个城市时的油量是有重要影响的,所以把状态设计成dp[i][j]表示到第i个城市剩余j油量的最小花费,然后状态转移是每次要么油量加1(加k的油量可以由k-1的油量的状态到达),要么走向相邻城市。注意这里的vis标记是出队的时候标记,而不是入队。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int,pair<int,int> > P;
typedef pair<int,int> PP;
const int maxn = 1000 + 5;
const int INF = 1000000000;
const int maxc = 100 + 5;

int p[maxn];

struct Edge{
    int to,dis;
    Edge(int to,int dis){
        this -> to = to;
        this -> dis = dis;
    }
};

vector<Edge> G[maxn];
priority_queue<P> Q;
int vis[maxn][maxc];
int s,e,c;

int bfs(){
    while(!Q.empty()){
        P p1 = Q.top();Q.pop();
        PP p2 = p1.second;
        int price = -p1.first;
        int pos = p2.first;
        int gas = p2.second;
        vis[pos][gas] = 1;
        if(pos == e) return price;
        if(gas < c && vis[pos][gas+1] == 0){
            Q.push(P(-(price+p[pos]),PP(pos,gas+1)));
        }
        for(int i = 0;i < G[pos].size();i++){
            int to = G[pos][i].to;
            int dis = G[pos][i].dis;
            if(dis <= gas && vis[to][gas-dis] == 0){
                Q.push(P(-price,PP(to,gas-dis)));
            }
        }
    }
    return -1;
}

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        for(int i = 0;i < n;i++) scanf("%d",&p[i]);
        for(int i = 0;i < n;i++) G[i].clear();
        for(int i = 0;i < m;i++){
            int u,v,d;
            scanf("%d%d%d",&u,&v,&d);
            G[u].push_back(Edge(v,d));
            G[v].push_back(Edge(u,d));
        }
        int q;
        scanf("%d",&q);
        while(q--){
            scanf("%d%d%d",&c,&s,&e);
            while(!Q.empty()) Q.pop();
            memset(vis,0,sizeof(vis));
            Q.push(P(0,PP(s,0)));
            int ans = bfs();
            if(ans == -1) printf("impossible\n");
            else printf("%d\n",ans);
        }
    }
    return 0;
}



您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值