CF1915G 题解

· · 题解

思路

这道题中,原本是最短路的板子题,但因为有 s_i 的变化,所以我们不能直接普通使用最短路。然后你会发现 n 和 s_i 都很小,只有 1000,因此我们可以把 d_i\to d_{i,j},其中 d_{i,j} 表示在第 i 个点且当前速度为 j 时最小的距离。然后 s_i 的话肯定是贪心取,取一路过来速度最快的自行车就行。

代码

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n, m, a[1005], x, y, z;
ll d[1005][1005];
vector <pii> v[1005];
struct node {
    ll dis;
    int u, minx;
    bool operator < (const node& cmp) const {
        return dis > cmp.dis;
    }
} ;
priority_queue <node> q;
ll dijkstra () {
    for (int i = 1; i <= n; ++ i)
        memset (d[i], 0x3f, sizeof d[i]);
    d[1][a[1]] = 0;
    q.push ({0, 1, a[1]});
    while (! q.empty ()) {
        const node t = q.top ();
        q.pop ();
        const int u = t.u, s = t.minx;
        if (t.dis > d[u][s])
            continue ;
        for (pii i : v[u]) {
            const int v = i.first, x = min (s, a[v]);
            if (t.dis + i.second * s < d[v][x])
                q.push ({d[v][x] = t.dis + i.second * s, v, x});
        }
    }
    ll minx = 1e18;
    for (int i = 0; i < 1003; ++ i)
        minx = min (minx, d[n][i]);
    return minx;
}
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> t;
    while (t --) {
        cin >> n >> m;
        for (int i = 1; i <= n; ++ i)
            v[i].clear ();
        while (m --) {
            cin >> x >> y >> z;
            v[x].push_back ({y, z});
            v[y].push_back ({x, z});
        }
        for (int i = 1; i <= n; ++ i)
            cin >> a[i];
        cout << dijkstra () << '\n';
    }
    return 0;
}