Cod sursa(job #2999210)

Utilizator pielevladutPiele Vladut Stefan pielevladut Data 10 martie 2023 17:17:38
Problema Pitici Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 1025;

int n, m, k;
int cnt[nmax + 5];
int best[nmax + 5][nmax + 5];
int cost[nmax + 5][nmax + 5];

struct elem
{
    int nod, idx, cost;
    bool operator < (const elem &other) const
    {
        return cost > other.cost;
    }
};
priority_queue<elem> coada;
vector<int> g[nmax + 5], invG[nmax + 5];

bool visited[nmax + 5];
vector<int> sortTop;

void dfs(int fiu)
{
    visited[fiu] = true;
    for(auto it : g[fiu])
    {
        if(visited[it] == true)
        {
            continue;
        }
        dfs(it);
    }
    sortTop.push_back(fiu);
}

int main()
{
    fin >> n >> m >> k;
    while(m--)
    {
        int x, y, z;
        fin >> x >> y >> z;
        g[x].push_back(y);
        invG[y].push_back(x);
        cost[x][y] = z;
    }
    dfs(1);
    reverse(sortTop.begin(), sortTop.end());
    best[1][1] = 0;
    cnt[1] = 1;
    for(int i = 1; i < n; i ++)
    {
        int nod = sortTop[i];
        for(auto it : invG[nod])
        {
            coada.push({it, 1, best[it][1] + cost[it][nod]});
        }
        while(!coada.empty() && cnt[nod] <= k)
        {
            int prevNod = coada.top().nod;
            int prevIdx = coada.top().idx;
            int distMin = coada.top().cost;
            coada.pop();
            best[nod][++cnt[nod]] = distMin;
            if(prevIdx + 1 <= cnt[prevNod])
            {
                coada.push({prevNod, prevIdx + 1, best[prevNod][prevIdx + 1] + cost[prevNod][nod]});
            }
        }
    }
    for(int i = 1; i <= k; i ++)
    {
        fout << best[n][i] << ' ';
    }
    fout << '\n';
    return 0;
}