Cod sursa(job #1801587)

Utilizator Alexa2001Alexa Tudose Alexa2001 Data 9 noiembrie 2016 12:33:49
Problema Pitici Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 1023;
typedef pair<int,int> Pair;

int n, m, x, y, cost, cnt[Nmax], k, i;
bool used[Nmax];
vector<int> path[Nmax];
vector<Pair> v[Nmax];

void dfs(int node)
{
    priority_queue< Pair, vector<Pair>, greater<Pair> > heap;
    int act, cost;
    used[node] = 1;

    for(auto it : v[node])
    {
        act = it.first;
        cost = it.second;

        if(!used[act]) dfs(act);

        if(!path[act].empty())
            heap.push({ path[act][0] + cost, act });

        cnt[act] = 0;
    }

    while(!heap.empty() && path[node].size() < k)
    {
        cost = heap.top().first;
        act = heap.top().second;
        heap.pop();

        path[node].push_back(cost);
        if(++cnt[act] < path[act].size())
            heap.push({ cost + path[act][cnt[act]] - path[act][cnt[act]-1], act });
    }
}

int main()
{
    ifstream zin("pitici.in");
    ofstream zout("pitici.out");

    zin >> n >> m >> k;

    while(m--)
    {
        zin >> x >> y >> cost;
        v[x].push_back({ y, cost });
    }

    path[n].push_back(0);
    dfs(1);

    for(i=0; i<k; ++i) zout << path[1][i] << " ";

    return 0;
}