Cod sursa(job #1913834)

Utilizator MailatMailat Radu Mailat Data 8 martie 2017 14:19:36
Problema Distante Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <bits/stdtr1c++.h>
#define maxn 50001
#define INF 50001
using namespace std;

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

int T, M, N, S;
int cost[maxn], D[maxn];
vector <pair <int, int> > A[maxn];
set < pair<int, int> > h;
vector <pair <int, int> > ::iterator it;
void graf(int S) {
    int to, cost, node;
    h.insert(make_pair(0,S));
    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));
            }
        }
    }
}

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 >> cost[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));
        }
        /*
        for(int j=0; j < A[S].size(); j++) {
            D[A[S][j].first] = A[S][j].second;
        }
        */
        D[S]=0;
        graf(S);

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

    }
}

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