Cod sursa(job #2521691)

Utilizator Mirela_MagdalenaCatrina Mirela Mirela_Magdalena Data 11 ianuarie 2020 12:47:43
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#define inf 0x3f3f3f3f
#define NMAX 50005
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream f("distante.in");
ofstream g("distante.out");


int t, n, m, st;
int Dmin[NMAX], Ddat[NMAX], ap[NMAX];
vector<pair<int, int> > graph[NMAX];
queue<int> Q;


void init()
{
    for(int i=1; i<=n; ++i)
        Dmin[i] = inf;
}




void read()
{
    int x, y, cost;

    f>>n>>m>>st;
    for(int i=1; i<=n; ++i)
        f>>Ddat[i];
    for(int i=1; i<=m; ++i)
    {
        f>>x>>y>>cost;
        graph[x].push_back({y, cost});
        graph[y].push_back({x, cost});
    }
}

void solve()
{
    Q.push(st);
    ap[st] = 1;
    Dmin[st] = 0;
    while(!Q.empty())
    {
        int de_unde = Q.front();
        Q.pop();
        for(auto &v:graph[de_unde])
            if(Dmin[v.first] > Dmin[de_unde] + v.second)
            {
                Dmin[v.first] = Dmin[de_unde] + v.second;
                if(Dmin[v.first] < Ddat[v.first])
                {
                    g<<"NU\n";
                    return;
                }
                if(ap[v.first] == 0)
                {
                    Q.push(v.first);
                    ap[v.first] = 1;
                }
            }
        ap[de_unde] --;
    }

    for(int i=1; i<=n; ++i)
        if(Dmin[i] != Ddat[i])
            {g<<"NU\n"; return;}
    g<<"DA\n";
}

void reset()
{
    while(!Q.empty())
        Q.pop();
    for(int i = 1; i<=n; ++i)
    {
        graph[i].clear();
        ap[i] = 0;
    }

    n = m = st = 0;

}


int main()
{
    f>>t;
    for(int i=0; i<t; ++i)
    {
        read();
        init();
        solve();
        reset();
    }

    return 0;
}