Cod sursa(job #2211363)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 9 iunie 2018 23:34:35
Problema Pitici Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back

using namespace std;

ifstream f ("pitici.in");
ofstream g ("pitici.out");

typedef pair <int, int> pii;
const int NMAX = 1024;
const int INF = 1 << 30;

int n, m, k, u, v, cost;
int dp[NMAX][NMAX], numPath[NMAX];
bool vis[NMAX];
vector <pii> adj[NMAX];


void dfs (int node) {
    vis[node] = true;
    for (int i = 0; i < k; ++i) {
        dp[node][i] = -INF;
    }
    if (node == n - 1) {
        dp[node][0] = 0;
        return;
    }
    priority_queue < pair <int , pair <int, int> > > q;
    while (!q.empty())
        q.pop();
    for (auto &i : adj[node]) {
        if (!vis[i.first]) {
            dfs(i.first);
        }
        numPath[i.first] = 0;
        q.push({dp[i.first][0] - i.second, {i.first, i.second}});
    }

    for (int i = 0; i < k && !q.empty(); ++i) {
        int dist = q.top().first;
        int adjNode = q.top().second.first;
        int cost = q.top().second.second;
        q.pop();
        dp[node][i] = dist;
        ++numPath[adjNode];
        q.push({dp[adjNode][numPath[adjNode]] - cost, {adjNode, cost}});
    }
}

int main() {
    f >> n >> m >> k;
    for (int i = 0; i < m; ++i) {
        f >> v >> u >> cost;
        --v; --u;
        adj[v].pb({u, cost});
    }
    dfs(0);
    for (int i = 0; i < k; ++i) {
        g << -dp[0][i] << ' ';
    }

    f.close();
    g.close();
    return 0;
}