Cod sursa(job #2449186)

Utilizator CharacterMeCharacter Me CharacterMe Data 18 august 2019 15:44:55
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>
#define pii pair<int, int>
///N=50000
///M=100000
///c=1000
///T=10
using namespace std;
int t, n, m, i, j, k, s;
int hypo[50001];
bool check[50001];
vector<pii> graph[50001];
queue<int> q;
///
int main()
{
    freopen("distante.in", "r", stdin);
    freopen("distante.out", "w", stdout);
    scanf("%d", &t);
    while(t--){
        while(!q.empty()) q.pop();
        for(i=1; i<=n; ++i){
            graph[i].clear();
            check[i]=false;
        }
        scanf("%d%d%d", &n, &m, &s);
        for(i=1; i<=n; ++i) scanf("%d", &hypo[i]);
        for(i=1; i<=m; ++i){
            int x, y, c;
            scanf("%d%d%d", &x, &y, &c);
            graph[x].push_back({y, c});
            graph[y].push_back({x, c});
        }
        if(!hypo[s]) {q.push(s); check[s]=true;}
        while(!q.empty()){
            int node=q.front(); q.pop();
            for(auto next:graph[node]){
                if(hypo[node]+next.second<hypo[next.first]) {check[node]=false; break;}
                if(hypo[node]+next.second==hypo[next.first] && !check[next.first]){
                    check[next.first]=true;
                    q.push(next.first);
                }
            }
            if(!check[node]) break;
        }
        for(i=1; i<=n; ++i) if(!check[i]){printf("NU\n"); break;}
        if(i==n+1) printf("DA\n");
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}