Cod sursa(job #2304984)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 18 decembrie 2018 22:04:42
Problema Pitici Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <bits/stdc++.h>

#define int_pair std::pair <int, int>

#define MAXN 1025
#define cnst 100
#define inf  200000005

int N, M, K;
std::vector <int_pair> ADC[MAXN];

inline void AddEdge(int x, int y, int w) {
    ADC[x].push_back({y, w});
    ADC[y].push_back({x, w});
}

std::ifstream In("pitici.in");
std::ofstream Out("pitici.out");

std::priority_queue <int_pair, std::vector <int_pair>, std::greater <int_pair>> PQ;
std::vector <int> Ans;
int Dist[MAXN];
int DP[MAXN];

void Dijkstra(int src = 1) {
    for (int i=1; i<=N; ++i)
        Dist[i] = inf;
    Dist[src] = 0;
    PQ.push({0, src});

    int_pair Top;
    while (!PQ.empty()) {
        Top = PQ.top();
        PQ.pop();

        if (Top.second == N) {
            Ans.push_back(Top.first);
            if (Ans.size() == K) return;
        }

        if (DP[Top.second] == K + cnst) continue;
        ++ DP[Top.second];
        for (auto Edge:ADC[Top.second])
            PQ.push({Top.first + Edge.second, Edge.first});
    }
}

void Citire() {
    In >> N >> M >> K;
    for (int i=0, x, y, w; i<M; ++i)
        In >> x >> y >> w, AddEdge(x, y, w);
}

void Rezolvare() {
    Dijkstra();
    for (int i=0; i<K; ++i)
        Out << Ans[i] << ' ' ;
}

int main()
{
    Citire();
    Rezolvare();

    return 0;
}