Cod sursa(job #1338425)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 9 februarie 2015 23:58:30
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;

ifstream is("distante.in");
ofstream os("distante.out");

#define INF 0x3f3f3f3f

int t, n, m, s, p;
int d[50001], a[50001];

vector<pair<int, int> > G[50001];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Q;

void Read();
void Solve(int x);

int main()
{
    is >> t;
    for ( int ct = 0; ct < t; ++ct )
    {
        p = 0;
        Read();
        Solve(s);
        for ( int i = 1; i <= n; ++i )
        {
            if ( d[i] == INF )
                d[i] = 0;
            if ( d[i] != a[i] )
            {
                p = 1;
                break;
            }
        }
        if ( p == 1 )
            os << "NU\n";
        else
            os << "DA\n";
    }
    is.close();
    os.close();
    return 0;
}
void Read()
{
    is >> n >> m >> s;
    for ( int i = 1; i <= n; ++i )
    {
        is >> a[i];
        G[i].clear();
    }
    for ( int i = 0, x, y, c; i < m; ++i )
    {
        is >> x >> y >> c;
        G[x].push_back({y, c});
        G[y].push_back({x, c});
    }
}
void Solve(int x)
{
    memset(d, 63, sizeof(d) );
    int nod;
    d[x] = 0;
    Q.push({0, x});
    while ( !Q.empty() )
    {
        nod = Q.top().first;
        x = Q.top().second;
        Q.pop();
        for (const auto& v : G[x] )
            if ( d[v.first] > nod + v.second )
            {
                d[v.first] = nod + v.second;
                Q.push({d[v.first], v.first});
            }
    }
}