Cod sursa(job #3269934)

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

using namespace std;
ifstream fin("pitici.in");
ofstream fout("pitici.out");
const int Nmax=1050;
bitset<Nmax> viz;
int dp[Nmax][Nmax];
vector<pair<int,int>> v[Nmax];
int k;
void dfs(int nod)
{
    viz[nod]=1;
    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;
            if(i==1 || dp[u][i]!=0)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 n,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});
    }
    dfs(1);
    for(int i=1;i<=k;i++) fout<<dp[1][i]<<' ';
    return 0;
}