Cod sursa(job #1444270)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 29 mai 2015 15:01:31
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <deque>
#include <cstring>

using namespace std;

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

const int NMax = 50005;
const int INF = 1e9;

int v[NMax], cost[NMax];
vector < pair < int, int > > G[NMax];
deque < int > stak;
bool viz[NMax];

void solve(){
    int node;
    pair < int, int > now;
    while(!stak.empty()){
        node = stak.front();
        stak.pop_front();
        for(int i = 0; i < G[node].size(); i++){
            now = G[node][i];
            if(cost[node] + now.second < cost[now.first]){
                cost[now.first] = cost[node] + now.second;
                if(!viz[now.first]){
                    stak.push_back(now.first);
                    viz[now.first] = 1;
                }
            }
        }
    }
}

inline bool good(const int &x){
    for(int i = 1; i <= x; i++){
        if(cost[i] != v[i]){
            return 0;
        }
    }
    return 1;
}

int main()
{
    int t, n, m, s, a, b, c;
    fin >> t;
    while(t--){
        fin >> n >> m >> s;
        memset(viz, 0, sizeof(viz));
        for(int i = 1; i <= n; i++){
            fin >> v[i];
            cost[i] = INF;
        }
        cost[s] = 0;
        for(int i = 1; i <= m; i++){
            fin >> a >> b >> c;
            G[a].push_back(make_pair(b, c));
            G[b].push_back(make_pair(a, c));
        }
        stak.push_back(s);
        viz[s] = 1;
        solve();
        if(good(n)){
            fout << "DA\n";
        } else {
            fout << "NU\n";
        }
        for(int i = 1; i <= n; i++){
            G[i].clear();
        }
    }
    return 0;
}