Cod sursa(job #3206135)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 21 februarie 2024 18:17:26
Problema Pitici Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 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;
    unsigned short cost;
    bool operator<(const cv& foreign) const {
        return this->cost > foreign.cost;
    }
};

short N,K;
int M;
vector<pair<short,unsigned short>> G[1020];
array<short,1020> viz;
priority_queue<cv> pq;
int cnt;
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,unsigned short>& edge : G[N]) {
        pq.push({edge.first, edge.second});
    }
    G[N].clear();
    while (!pq.empty()) {
        if (pq.top().node == 1) {
            fout << pq.top().cost << ' ';
            cnt++;
            if (cnt == K) {
                break;
            }
        } else {
            for (const pair<short,unsigned short>& edge : G[pq.top().node]) {
                if (viz[edge.first] <= K + 20000) {
                    pq.push({edge.first,static_cast<unsigned short>(edge.second + pq.top().cost)});
                    viz[edge.first]++;
                }
            }
        }
        pq.pop();
    }

    return 0;
}