Cod sursa(job #2116742)

Utilizator CronosClausCarare Claudiu CronosClaus Data 27 ianuarie 2018 21:55:59
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <bits/stdc++.h>
#define x first
#define y second
#define pp pair< int, int >

using namespace std;

const int mxn = 50 * 1000 + 10;

int n_query;

int n, m, s;
int dist_q[ mxn ], dist[ mxn ];

void dijkstra(vector< pp > v[]){
    int nod, c;
    priority_queue< pp, vector< pp >, greater< pp > > q;
    dist[ s ] = 0;
    q.push(make_pair(0, s));
    while(!q.empty()){
        nod = q.top().y;
        c = q.top().x;
        q.pop();
        if(c > dist[ nod ])
            continue;
        for(auto it: v[ nod ])
            if(dist[ it.x  ] > c + it.y){
                dist[ it.x ] = c + it.y;
                q.push(make_pair(dist[ it.x ], it.x));
            }
    }
}

int main()
{
    ios_base::sync_with_stdio( false );
    cin.tie( 0 );
    ifstream cin("distante.in");
    ofstream cout("distante.out");
    cin>> n_query;
    for(int query = 1; query <= n_query; query++){
        cin>> n >> m >> s;
        for(int i = 1; i <= n; i++){
            cin>> dist_q[ i ];
            dist[ i ] = INT_MAX;
        }
        vector< pp > v[ n + 10 ];
        for(int i = 1, xx, yy, zz; i <= m; i++){
            cin>> xx >> yy >> zz;
            v[ xx ].push_back(make_pair(yy, zz));
            v[ yy ].push_back(make_pair(xx, zz));
        }
        dijkstra(v);
        for(int i = 1; i <= n; i++)
            if(dist[ i ] == INT_MAX)
                dist[ i ] = 0;
        bool ok = 1;
        for(int i = 1; i <= n; i++)
            ok *= (dist[ i ] == dist_q[ i ]);
        if(ok)
            cout<< "DA\n";
        else
            cout<< "NU\n";
    }
    return 0;
}