Cod sursa(job #3145052)

Utilizator NanuGrancea Alexandru Nanu Data 12 august 2023 13:24:59
Problema Pitici Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <queue>
#include <vector>
#include <set>

using namespace std;

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

const int NMAX = 1020;

int n, m, k;
int level[NMAX + 5], viz[NMAX + 5];
multiset <int> M[NMAX + 5];
vector <pair<int, int>> G[NMAX + 5];
priority_queue <pair<int, int>> PQ;

static inline void bfs() {
  PQ.push({1, 1});
  M[1].insert(1);
  while(!PQ.empty()) {
    int nod = PQ.top().second;
    int d = PQ.top().first;
    PQ.pop();

    for(auto e : G[nod]) {
        PQ.push({d + e.second, e.first});
        M[e.first].insert(d + e.second);
        while(M[e.first].size() > k) {
          M[e.first].erase(--M[e.first].end());
        }
      }
  }
}

int main() {
  fin.tie(0), fout.tie(0);

  fin >> n >> m >> k;
  for(int i = 1, x, y, z; i <= m; i++) {
    fin >> x >> y >> z;
    G[x].push_back({y, z});
  }

  bfs();

  for(auto e : M[n]) 
    fout << e - 1 << " ";


  return 0;
}