Cod sursa(job #3206138)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 21 februarie 2024 18:19:58
Problema Pitici Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 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()) {
        const cv mem = pq.top();
        pq.pop();
        if (mem.node == 1) {
            fout << mem.cost << ' ';
            cnt++;
            if (cnt == K) {
                break;
            }
        } else {
            for (const pair<short,unsigned short>& edge : G[mem.node]) {
                if (viz[edge.first] <= K + 20000) {
                    pq.push({edge.first,static_cast<unsigned short>(edge.second + mem.cost)});
                    viz[edge.first]++;
                }
            }
        }
    }

    return 0;
}