Cod sursa(job #2528447)

Utilizator i.uniodCaramida Iustina-Andreea i.uniod Data 21 ianuarie 2020 21:25:23
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

const int INF = 2e9;
const int NMAX = 5e4;

struct Node {

    int node, cost;

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


int N, M, T, S;
int dist[NMAX+5];
int var[NMAX+5];
vector <Node> G[NMAX+5];
priority_queue <Node> pq;

void Dijkstra(int start)
{
    for(int i = 1; i <= N; ++ i)
        dist[i] = INF;

    pq.push({start,0});

    while(!pq.empty()) {
        int cnode = pq.top().node;
        int cdist = pq.top().cost;
        pq.pop();

        if(dist[cnode] != INF)
            continue;

        dist[cnode] = cdist;

        for(auto it : G[cnode])
            if(dist[it.node] == INF)
                pq.push({it.node, cdist + it.cost});
    }

    for(int i = 1; i <= N; ++ i)
        if(dist[i] == INF)
            dist[i] = 0;
}

int main()
{
    fin >> T;
    for(int p = 1; p <= T; ++ p) {

        fin >> N >> M >> S;
        for(int i = 1; i <= N; ++ i)
            fin >> var[i];
        int a, b, c;
        for(int i = 1; i<= M; ++ i) {
            fin >> a >> b >> c;
            G[a].push_back({b,c});
            G[b].push_back({a,c});
        }

        Dijkstra(S);

        bool ok = true;
        for(int i = 1; i <= N; ++ i)
            if(var[i] != dist[i])
                ok = false, i = N;

        if(ok == true)
            fout << "DA" << '\n';
        else
            fout << "NU" << '\n';
    }
    return 0;
}