Cod sursa(job #731697)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 8 aprilie 2012 21:56:30
Problema Pitici Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

const int N = 1030;

priority_queue <pair <int, short int>, vector <pair <int, short int> >, greater <pair <int, short int> > > H;

vector <pair <int, int> > L[N];

vector <int> dist[N];

int n, k;

void read() {
  int m, x, y, c;

  scanf("%d%d%d", &n, &m, &k);

  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &x, &y, &c);
    L[x].push_back(make_pair(y, c));
  }
}

void solve() {
  pair <int, short int> ncs;
  pair <short int, int> nc;

  H.push(make_pair(0, 1));

  while (!H.empty()) {
    ncs = H.top();
    nc.first = ncs.second;
    nc.second = ncs.first;
    H.pop();

    if ((int)dist[nc.first].size() >= k)
      continue;

    dist[nc.first].push_back(nc.second);
    
    for (int i = 0; i < (int)L[nc.first].size(); ++i) {
      if ((int)dist[L[nc.first][i].first].size() >= k)
        continue;

      H.push(make_pair(nc.second + L[nc.first][i].second, L[nc.first][i].first));
    }
  }
}

void print() {
  for (int j = 0; j < k; ++j)
    printf("%d ", dist[n][j]);
}

int main() {
  freopen("pitici.in", "r", stdin);
  freopen("pitici.out", "w", stdout);

  read();

  solve();

  print();

  return 0;
}