Cod sursa(job #1913963)

Utilizator MailatMailat Radu Mailat Data 8 martie 2017 14:52:48
Problema Distante Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <bits/stdtr1c++.h>
#define maxn 50005
#define INF 16e5
using namespace std;

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

int T, M, N, S, to, cost;
int d[maxn], D[maxn];
vector <pair <int, int> > A[maxn];
set < pair<int, int> > h;
vector <pair <int, int> > ::iterator it;
void init() {
    int x, y,c;
    bool match = true;
    fin >> T;
    for(int i=0; i < T; i++) {
        fin >> N >> M >> S;
        for(int j=1; j<=N; j++) {
            A[j].clear();
            fin >> d[j];
            D[j] = INF;
        }
        for(int j=0; j < M; j++) {
            fin >> x >> y >> c;
            A[x].push_back(make_pair(y,c));
            A[y].push_back(make_pair(x, c));
        }
        D[S]=0;
        h.insert(make_pair(0,S));
        int node;
        while(!h.empty()) {
            node = h.begin()->second;
            h.erase(h.begin());
            for(it = A[node].begin(); it != A[node].end(); ++it) {
                to = it->first;
                cost = it->second;
                if(D[to] > D[node] + cost) {
                    if(D[to] != INF) h.erase(h.find(make_pair(D[to],to)));
                    D[to] = D[node] + cost;
                    h.insert(make_pair(D[to], to));
                }
            }
        }

        for(int j=1; j <= N; j++) {
            if(D[j] != d[j]){
                match = false;
                break;
            }
        }
        if(match) fout << "DA" << endl;
        else fout << "NU" << endl;

    }
}

int main()
{
    init();
    return 0;
}