Cod sursa(job #2154529)

Utilizator chioreanraulChiorean Raul chioreanraul Data 7 martie 2018 00:25:17
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>

#define cost first
#define nod second

using namespace std;
int t,i,j,s,n,m,fv[50005],val[50005],c,verific;

vector< pair < int, int > >v[50005];
priority_queue< pair< int, int >, vector < pair < int,int > >,greater < pair < int,int > > >pq;
pair< int,int > aux;

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

void dijkstra ( int inc )
{
    pq.push( { 0 , inc } );
    while( !pq.empty( ) )
    {
        aux = pq.top( );
        pq.pop( );
        if( fv[ aux.nod ] )
            continue;
        fv[ aux.nod ] = aux.cost;
        for( int r = 0;r < v[ aux.nod ].size( ); r++ )
        {
            pq.push( { aux.cost + v[ aux.nod ][ r ].cost, v[ aux.nod ][ r ].nod });
        }

    }
}
bool verif(int frv, int valo )
{
    if( frv == valo )
        return true;
    else
        return false;
}
int main()
{
    fin>>t;
    for( int k = 1; k <= t; k++ )
    {
        fin>>n>>m>>s; verific = 1;
        for( int r = 1; r <= n; r++ )
            fin>> val[ r ];
        for( int r = 1; r <= m; r++ )
        {
            fin>>i>>j>>c;
            v[ i ].push_back( { c, j } );
            v[ j ].push_back( { c, i } );
        }
        dijkstra( s );
        for( int r = 1; r <= n; r++ )
        {
            if( s != r )
            verific = verific * verif( fv[ r ], val[ r ] );
        }

        if( verific == 1 )
            fout<<"DA\n";
        else
            fout<<"NU\n";
        memset( fv, 0 , sizeof( fv ) );
        for( int r = 1; r <= n; r++ )
        {
            v[ r ].clear( );
        }
    }
    return 0;
}