Cod sursa(job #2500825)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 28 noiembrie 2019 19:08:34
Problema Pitici Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("pitici.in");
ofstream fout("pitici.out");

struct Quadruple {
    int x, y, z, t;
    Quadruple(int x, int y, int z, int t) :
        x(x), y(y), z(z), t(t) { }
    inline bool operator>(Quadruple q) const {
        return x > q.x;
    }
};

class Graph {
  private:
    int n;
    vector<vector<pair<int, int>>> ad;

    void dfs(int node, vector<bool>& vis, vector<int>& topo) {
        vis[node] = true;
        for (auto nghb : ad[node])
            if (!vis[nghb.first])
                dfs(nghb.first, vis, topo);
        topo.push_back(node);
    }

    vector<int> topoSort() {
        vector<int> topo;
        vector<bool> vis(n + 1);
        for (int i = 1; i <= n; i++)
            if (!vis[i])
                dfs(i, vis, topo);
        return topo;
    }

  public:
    Graph(int n) :
        n(n), ad(n + 1) { }

    void addEdge(int x, int y, int cost) {
        ad[x].emplace_back(y, cost);
    }

    void solve(int k) {
        vector<int> topo = topoSort();
        vector<vector<int>> dp(n + 1, vector<int>(k + 1, 1e9));

        for (int i : topo)
            if (ad[i].empty())
                dp[i][1] = 0;
            else {
                priority_queue<Quadruple, vector<Quadruple>, greater<Quadruple>> pq;
                for (auto j : ad[i])
                    pq.emplace(dp[j.first][1] + j.second, j.first, 1, j.second);
                for (int j = 1; j <= k; j++) {
                    auto top = pq.top(); pq.pop();
                    dp[i][j] = top.x;
                    if (top.z < k)
                        pq.emplace(dp[top.y][top.z + 1] + top.t, top.y, top.z + 1, top.t);
                }
            }

        for (int j = 1; j <= k; j++)
            fout << dp[1][j] << ' ';
        fout << '\n';
    }
};

int main() {
    int n, m, k;
    fin >> n >> m >> k;

    Graph graph(n);
    for (int i = 0; i < m; i++) {
        int x, y, z; fin >> x >> y >> z;
        graph.addEdge(x, y, z);
    }
    graph.solve(k);

    fout.close();
    return 0;
}