Cod sursa(job #2102949)

Utilizator dragomirmanuelDragomir Manuel dragomirmanuel Data 9 ianuarie 2018 17:34:39
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <iostream>
#include <cstdio>
#include <queue>
#include <bitset>
#include <vector>

#define mp make_pair
#define pb push_back

using namespace std;

const int NMax = 50005;
const int inf = 0x3f3f3f3f;
int T, N, M, S;
int d[NMax], dist[NMax];
vector < pair < int , int > > G[NMax];
priority_queue < pair < int ,int > > Q;
bitset < NMax > viz;

int Check()
{
    for(int i=1; i<=N; ++i)
        viz[i] = 0;

    for(int i=1; i<=N; ++i)
        dist[i] = inf;

    dist[S] = 0;
    viz[S] = 1;
    Q.push(mp(0,S));

    while(!Q.empty())
    {
        int nodc = Q.top().second;
        int costc = -Q.top().first;

        if(costc != dist[nodc])
            continue;

        Q.pop();

        vector < pair < int, int > > ::iterator it;
        for(it=G[nodc].begin(); it!=G[nodc].end(); ++it)
            if(!viz[it->first] && costc + it->second <= dist[it->first])
        {
            if(dist[it->first] < d[it->first])
                return 0;



            dist[it->first] = dist[nodc] + it->second;
            Q.push(mp(-dist[it->first],it->first));
            viz[it->first] = 1;
        }
    }

    for(int i=1; i<=N; ++i)
        if(dist[i] != d[i])
            return 0;

    return 1;
}

int main()
{
    freopen("distante.in", "r", stdin);
    freopen("distante.out", "w", stdout);

    scanf("%d", &T);

    for(int i=1; i<=T; ++i)
    {
        scanf("%d %d %d", &N, &M, &S);

        for(int j=1; j<=N; ++j)
            scanf("%d", &d[j]);

        for(int j=1; j<=M; ++j)
        {
            int x, y, c;
            scanf("%d %d %d", &x, &y, &c);
            G[x].pb(mp(y,c));
            G[y].pb(mp(x,c));
        }

        if(Check())
            printf("DA\n");

        else
            printf("NU\n");
    }

    return 0;
}