Cod sursa(job #2525737)

Utilizator MichaelXcXCiuciulete Mihai MichaelXcX Data 17 ianuarie 2020 18:46:45
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 100005;
const int INF = 0x3f3f3f;

int T, N, M, S, a, b, c;
int Dist[NMAX], InitialDist[NMAX], checked[NMAX];

struct Edge {
    int next;
    int cost;
};

vector< Edge > G[NMAX];
queue< int > Q;

void Solution()
{
    fin >> N >> M >> S;
    S--;

    for(int i = 0; i < N; ++i)
        fin >> InitialDist[i];

    for(int i = 0; i < M; ++i) {
        fin >> a >> b >> c;

        G[a - 1].push_back({b - 1, c});
        G[b - 1].push_back({a - 1, c});
    }

    memset(Dist, INF, sizeof(Dist));

    Dist[S] = 0;
    Q.push(S);
    checked[S] = 1;

    while(!Q.empty()) {
        auto currentNode = Q.front();
        Q.pop();
        checked[currentNode] = 0;

        for(auto m : G[currentNode]) {
            if(Dist[currentNode] + m.cost < Dist[m.next]) {
                Dist[m.next] = Dist[currentNode] + m.cost;

                if(!checked[m.next]) {
                    Q.push(m.next);
                    checked[m.next] = 1;
                }
            }
        }
    }

    for(int i = 0; i < N; i++) {
        if(Dist[i] != InitialDist[i]) {
            fout << "NU\n";
            return;
        }
    }
    fout << "DA\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);

    fin >> T;

    while(T--) {
        Solution();

        //Dijkstra(S);
        //(isMatch()) ? fout << "DA\n" : fout << "NU\n";
    }
}