Cod sursa(job #3285207)

Utilizator ShAwDoRneYNacu Gabriel ShAwDoRneY Data 12 martie 2025 16:48:39
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream fin("distante.in");
ofstream fout("distante.out");

struct Muchie {

    int to, cost;

    bool operator<(const Muchie &other) const {
        return cost > other.cost;
    }
};

void DIJKSTRA(int N, int Source, vector<int> &check, vector<vector<Muchie>> &graph) {

    priority_queue<Muchie> Q;
    vector<int> dist(N+1, 1e9);
    Q.push({Source, 0});
    dist[Source] = 0;

    while(!Q.empty()) {

        int node = Q.top().to;
        int cost = Q.top().cost;
        Q.pop();

        if(cost > dist[node])
            continue;

        for(auto neighbor : graph[node]) {
            int vecin = neighbor.to;
            int lungime = neighbor.cost;

            if(cost + lungime < dist[vecin]) {
                dist[vecin] = cost + lungime;
                Q.push({vecin, dist[vecin]});
            }
        }
    }

    for(int i=1; i<=N; i++) {
        if(dist[i] != check[i]) {
            fout << "NU" << "\n";
            return;
        }
    }
    fout << "DA" << "\n";
}

int main() {

    int T;
    fin >> T;

    while(T--) {
        int N,M,S;
        fin >> N >> M >> S;
        vector<vector<Muchie>> graph(N+1, vector<Muchie>());
        vector<int> check(N+1);
        for(int i=1; i<=N; i++)
            fin >> check[i];
        
        for(int i=1; i<=M; i++) {
            int from,to,cost;
            fin >> from >> to >> cost;
            graph[from].push_back({to, cost});
            graph[to].push_back({from, cost});
        }

        DIJKSTRA(N, S, check, graph);

    }

    return 0;
}