Magic Girl Haze

T组
n个点,m条有向含权边,可以选择不超过k条边,将其权值变为0.
问点1到点n的最短距离是多少?
1 T 5 n 1 0 5 m 2 × 1 0 5 k 10 w i 1 0 9 1\leq T \leq 5 \\ n \leq 10^5 \\ m \leq 2\times10^5 \\ k \leq 10 \\ w_i \leq 10^9 1T5n105m2×105k10wi109

二维状态Dijkstra

原本dijkstra算法是dis[v],只有点的编号一个维度,dis[v]状态表示的是1到v最短的距离。
现在令dis[k][v]表示恰好k条边变0,点1到v最短的距离,状态变为二维
原本是 u u u v v v有边就可以尝试状态转移
现在是

  1. ( k , u ) (k,u) (k,u) ( k , v ) (k,v) (k,v)可以转移,代价w;
  2. ( k , u ) (k,u) (k,u) ( k + 1 , u ) (k+1,u) (k+1,u)可以转移,代价0;

正确性是显然的
对于状态 k = k 0 , u ( k , u ) k = k_0,\forall u(k,u) k=k0,u(k,u),假设它们的初始值已经设置好了,则固定 k = k 0 k=k_0 k=k0,它们之间的转移更新就是传统的一维dijkstra,因此是正确的
k = k 0 , u ( k , u ) k = k_0,\forall u (k,u) k=k0,u(k,u)初始值本是无穷大, 但是 k = K 0 1 k=K_0-1 k=K01的状态可以优化它们的初始值。

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n, m;
int k;
const int maxn = 1e+5 + 5;
const int maxm = 2e+5 + 5;
const int maxk = 10;
vector<int> ev[maxn];
vector<ll> ew[maxn];
// bool solved[maxk + 1][maxn];
bool vis[maxk + 1][maxn];
ll dis[maxk + 1][maxn];
struct vnd
{
    int v;
    ll dis;
    bool operator<(const struct vnd &obj) const
    {
        return dis > obj.dis;
    }
    void print() const {
        cout << "(v,dis) = "<<v<<","<<dis<<endl;
    }
};
priority_queue<vnd> q[maxk + 1];

void init()
{
    cin >> n >> m >> k;
    for (int u = 1; u <= n; ++u)
    {
        ev[u].clear();
        ew[u].clear();
    }
    int u, v, w;
    for (int i = 1; i <= m; ++i)
    {
        cin >> u >> v >> w;
        ev[u].push_back(v);
        ew[u].push_back(w);
    }
    // memset(solved, false, sizeof(solved));
    memset(vis, false, sizeof(vis));
    for (int i = 0; i <= k; ++i)
        q[i] = priority_queue<vnd>();
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> t;
    while (t--)
    {
        init();
        bool first = true;
        ll ans;
        vis[0][1] = true;
        dis[0][1] = 0;
        int u, v;
        ll w;
        for (int i = 0; i <= k; ++i)
        {
            // push visited vertices into queue
            for (int u = 1; u <= n; ++u)
                if (vis[i][u]) {
                    q[i].emplace(vnd{u,dis[i][u]});
                }
            while (!q[i].empty())
            {
                vnd t = q[i].top();
                q[i].pop();
                u = t.v;
                for (size_t j = 0; j < ev[u].size(); ++j)
                {
                    v = ev[u][j];
                    w = ew[u][j];
                    // do not change w
                    if (!vis[i][v])
                    {
                        vis[i][v] = true;
                        dis[i][v] = t.dis + w;
                        q[i].emplace(vnd{v,dis[i][v]});
                    }
                    else if (dis[i][v] > t.dis + w)
                    {
                        dis[i][v] = t.dis + w;        
                        q[i].emplace(vnd{v,dis[i][v]});
                    }
                    // change w to 0
                    if (i + 1 > k)
                        continue;
                    if (!vis[i + 1][v])
                    {
                        vis[i + 1][v] = true;
                        dis[i + 1][v] = t.dis;
                    }
                    else if (dis[i + 1][v] > t.dis)
                    {
                        dis[i + 1][v] = t.dis;
                    }
                }
            }
            if (vis[i][n])
            {
                if (first)
                {
                    first = false;
                    ans = dis[i][n];
                }
                else
                {
                    ans = min(ans, dis[i][n]);
                }
            }
        }
        // the input ensure: first == false
        cout << ans << endl;
    }
    return 0;
}