UVA 11367 Full Tank? (dij+heap+dp)

题意:给定一张 n 个点m条边的无向图,每条边有个距离 D(i,j) ,车辆每经过一个单位的距离消耗一单位的油。,有一辆车从点 start 出发前往 end ,车的油箱容量为 c ,在每个节点Vi上车辆可以以 Pi 的价格补充 1 单位的油,现在给定q次询问,每次询问给定出发点 start ,终点 end ,油箱容量 c ,问能否从起点出发到达终点,并输出最短距离

思路:最短路上的dp,如果求出所有的路径,那么我们需要做一次dp或者贪心来得到最小值,但实际上我们可以使用用heap优化的dij算法,以最小花费为权重,每次加一升油,来判断当前这个状态最远能走到那里。使用dp[u][j]表示在 Vu 还剩 j 升油的最小花费,对于每个状态更新与Vi相连的点的权重,状态转移方程为

dp[Vv][jD(u,v)]=min(dp[Vv][jD(u,v)],dp[Vu][j])

同时,在更新完成之后,尝试在当前点加油,状态转移方程为
dp[Vu][j+1]=min(dp[Vu][j+1],dp[Vu][j]+Pu)

总的时间复杂度为

O(T)=O(qcnlogm)

#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cstring>
#include <cstdlib>
#include <set>
#include <algorithm>
#include <cctype>
#include<cmath>
#include <sstream>

using namespace std;
#define sp system("pause");
typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1050;
const double PI = acos(-1.0);

vector<pii>e[MAXN];

int pri[MAXN];
int dp[MAXN][120];

struct node
{
    int p, c, w;
    node(){}
    node(int a, int b, int c) :p(a), c(b), w(c){}
    friend bool operator<(const node &x, const node &y)
    {
        return x.w > y.w;
    }
};

int dij(int st, int en, int cap)
{
    for (int i = 0; i < MAXN; i++)for (int j = 0; j < 120; j++)dp[i][j] = 100000000;
    priority_queue<node>pq;
    dp[st][0] = 0;
    pq.push(node(st, 0, 0));
    while (!pq.empty())
    {
        node tmp = pq.top();
        pq.pop();
        if (tmp.p == en)return tmp.w;
        int u = tmp.p;
        for (int i = 0; i < e[tmp.p].size(); i++)
        {
            int v = e[u][i].first;
            int d = e[u][i].second;
            if (tmp.c-d>=0&&dp[v][tmp.c - d]>tmp.w)
            {
                dp[v][tmp.c - d] = tmp.w;
                pq.push(node(v, tmp.c - d, tmp.w));
            }
        }
        if (tmp.c < cap&&dp[u][tmp.c+1]>tmp.w+pri[u])
        {
            dp[u][tmp.c + 1] = tmp.w + pri[u];
            pq.push(node(u, tmp.c + 1, dp[u][tmp.c + 1]));
        }
    }
    return -1;
}

int main()
{
    int n, m;
    while (scanf("%d%d", &n, &m) != EOF)
    {
        for (int i = 0; i < MAXN; i++)e[i].clear();
        for (int i = 0; i < n; i++)scanf("%d", pri + i);
        for (int i = 0; i < m; i++)
        {
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            e[x].push_back(pii(y, z));  
            e[y].push_back(pii(x, z));
        }
        int q;
        scanf("%d", &q);
        while (q--)
        {
            int c, st, en;
            scanf("%d%d%d", &c, &st, &en);
            int ans = dij(st, en, c);
            if (ans == -1)puts("impossible");
            else printf("%d\n", ans);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值