Cod sursa(job #3206128)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 21 februarie 2024 18:13:18
Problema Pitici Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

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

struct cv {
    short node;
    int cost;
    bool operator<(const cv& foreign) const {
        return this->cost > foreign.cost;
    }
};

short N,K;
int M;
vector<pair<short,short>> G[1020];
array<short,1020> viz;
priority_queue<cv> pq,ans;
int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fin >> N >> M >> K;
    for (int i = 1; i <= M; i++) {
        short nodeA, nodeB, cost;
        fin >> nodeA >> nodeB >> cost;
        G[nodeB].push_back({nodeA,cost});
    }
    for (const pair<short,short>& edge : G[N]) {
        pq.push({edge.first, edge.second});
    }
    G[N].clear();
    while (!pq.empty()) {
        if (pq.top().node == 1) {
            ans.push(pq.top());
            if (ans.size() == K) {
                break;
            }
        } else {
            for (const pair<short,short>& edge : G[pq.top().node]) {
                if (viz[edge.first] <= K + 20000) {
                    pq.push({edge.first, edge.second + pq.top().cost});
                    viz[edge.first]++;
                }
            }
        }
        pq.pop();
    }
    while (K--) {
        fout << ans.top().cost << ' ';
        ans.pop();
    }
    return 0;
}