Cod sursa(job #3269935)

Utilizator tudor_costinCostin Tudor tudor_costin Data 21 ianuarie 2025 15:29:21
Problema Pitici Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("pitici.in");
ofstream fout("pitici.out");
const int Nmax=1050,inf=1e9;
bitset<Nmax> viz;
int dp[Nmax][Nmax];
vector<pair<int,int>> v[Nmax];
int n,k;
void dfs(int nod)
{
    viz[nod]=1;
    if(nod==n)
    {
        dp[nod][1]=0;
        return;
    }
    if(v[nod].empty()) return;
    priority_queue<pair<int,int>> pq;
    bitset<Nmax> bad;
    for(auto pi:v[nod])
    {
        int u=pi.first;
        if(viz[u]) continue;
        dfs(u);
    }
    priority_queue<pair<int,int>> ans;
    for(int i=1;i<=k;i++)
    {
        for(auto pi:v[nod])
        {
            int u=pi.first;
            int c=pi.second;
            if(bad[u]) continue;
            ans.push({dp[u][i]+c,u});
            if(ans.size()>k)
            {
                bad[ans.top().second]=1;
                ans.pop();
            }
        }
    }
    int id=ans.size();
    while(!ans.empty())
    {
        dp[nod][id]=ans.top().first;
        ///cout<<nod<<' '<<id<<' '<<dp[nod][id]<<'\n';
        ans.pop();
        id--;
    }
    return;
}
int main()
{
    int m;
    fin>>n>>m>>k;
    for(int i=1;i<=m;i++)
    {
        int x,y,c;
        fin>>x>>y>>c;
        v[x].push_back({y,c});
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=k;j++) dp[i][j]=inf;
    }
    dfs(1);
    for(int i=1;i<=k;i++) fout<<dp[1][i]<<' ';
    return 0;
}