Waver's Flight
Difficulty: Hard

Waver needs to get out of the Clock Tower immediately. The Clock Tower is a tree of $N$ vertices numbered $1$ through $N$. Additionally, there are $E$ shortcut edges. Using shortcuts arouses suspicion, therefore Waver has decided that he will use shortcuts at most $K$ times. He needs to get to an exit to leave the place. All leaf vertices are exit nodes. Each edge, normal and shortcut, is weighted, which indicates the time taken to traverse it. 

Waver wants to exit the Clock Tower as soon as possible. Print the least units of time Waver needs to reach an exit node from every vertex.

Input Format:
The first line contains three integers: $N$ (the number of vertices), $E$ (the number of shortcut edges), and $K$ (the number of times shortcut edges can be used).
 
The next $N-1$ lines contain three integers, $a$, $b$, and $w$ each, indicating that vertices $a$ and $b$ are connected by a tree edge of weight $w$.
 
$E$ lines follow, each having three integers, $a$, $b$, and $w$, such that $a$ and $b$ are connected by a shortcut edge of weight $w$.

**Constraints**  
$1 \le N \le 10000$  
$0 \le E \le 20000$  
$0 \le K \le 1000$

For all edges: $1 \le w \le 1000$  
There is at most one shortcut edge between any pair of vertices.  
There is no shortcut from a vertex to itself.  
All edges are bidirectional.  
Note that two vertices can be connected both by a tree edge and a shortcut edge.

Output Format:
Output must consist of $N$ lines, where the $i$<sup>$th$</sup> line has a single integer denoting the least units of time Waver needs to the exit from vertex $i$.

Sample Input:
    6 2 1
    1 5 25
    5 4 1
    6 4 11
    4 3 10
    2 3 100
    3 4 7
    5 1 3

Sample Output:
    0
    0
    14
    4
    3
    0

Python 3 starter code:


Reference solution (C++):
    #include <iostream>
    #include <cassert>
    #include <cstdio>
    #include <vector>
    #include <tuple>
    using namespace std;

    #define inc(i,a,b)              for(int i=a;i<=b;++i)
    #define dec(i,a,b)              for(int i=a;i>=b;--i)

    #define pb                      push_back
    #define mt                      make_tuple

    typedef long long ll;

    typedef tuple<int,int> ii;

    typedef vector<int> vi;
    typedef vector<ii> vii;

    const int maxN =1e4, maxK = 1e3, maxW = 1e3;
    const int maxE = 2*maxN;
    const int inf = 1e9;

    int n, e, k;

    vii T[maxN+1], S[maxN+1];
    int D[maxN+1][maxK+1];

    void input();
    void solve();
    void dfs1(int=0,int=1,int=-1);
    void dfs2(int=0,int=1,int=-1);

    int main() {
        ios::sync_with_stdio(false);
        cin.tie(NULL);

        input();
        solve();
    }

    void input()
    {
        cin >> n >> e >> k;

        assert(n<=maxN);
        assert(e<=maxE);
        assert(k<=maxK);

        inc(i,1,n-1)
        {
            int a, b, w;
            cin >> a >> b >> w;

            assert(a>=1 && a<=n);
            assert(b>=1 && b<=n);
            assert(w>=0 && w<=maxW);

            T[a].pb(mt(b,w));
            T[b].pb(mt(a,w));
        }
        inc(i,1,e)
        {
            int a, b, w;
            cin >> a >> b >> w;

            S[a].pb(mt(b,w));
            S[b].pb(mt(a,w));
        }
    }
    void solve()
    {
        inc(u,1,n)
        {
            if(T[u].size() < 2)
                D[u][0] = 0;
            else
                D[u][0] = inf;
        }

        dfs1();
        dfs2();

        inc(s,1,k)
        {
            inc(u,1,n)
            {
                D[u][s] = D[u][s-1];
                for(auto x : S[u])
                {
                    int v, w;
                    tie(v,w) = x;

                    D[u][s] = min(D[u][s],D[v][s-1]+w);
                }
            }

            dfs1(s);
            dfs2(s);
        }

        inc(u,1,n)
        {
            cout << D[u][k] << "\n";
        }
    }
    void dfs1(int s, int u, int p)
    {
        for(auto x : T[u])
        {
            int v, w;
            tie(v,w) = x;

            if(v == p)
                continue;

            dfs1(s,v,u);

            D[u][s] = min(D[u][s],D[v][s]+w);
        }
    }
    void dfs2(int s, int u, int p)
    {
        for(auto x : T[u])
        {
            int v, w;
            tie(v,w) = x;

            if(v == p)
                continue;

            D[v][s] = min(D[v][s],D[u][s]+w);

            dfs2(s,v,u);
        }
    }