Cod sursa(job #1356466)

Utilizator killlerr1Chilom Mircea killlerr1 Data 23 februarie 2015 14:06:16
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

const int MAXN = 50001, INF = 10001;

struct Edge{
    int nod, cost;

    const bool operator < (const Edge& e) const
    {
        return cost > e.cost;
    }
};

vector<vector<Edge> > G;
int t, n, m, s;
int d1[MAXN];
string str;
int d2[MAXN];
bool vis[MAXN];

priority_queue<Edge> Q;
bool ok;

void Dijkstra(int nod);

int main()
{
    is >> t;
    while(t)
    {
        ok = true;
        is >> n >> m >> s;
        G.resize(n+1);
        for( int i = 1; i <= n; ++i )
            is >> d1[i];
        int a, b, c;
        for( int i = 1; i <= m; ++i )
        {
            is >> a >> b >> c;
            G[a].push_back({b, c});
            G[b].push_back({a, c});

        }
        Dijkstra(s);
        for( int i = 1; i <= n; ++i )
            if( d1[i] != d2[i] )
                ok = false;
        if( ok == true )
            str ="DA";
        else
            str ="NU";
        os << str << '\n';
        --t;
    }


    is.close();
    os.close();
    return 0;
}


void Dijkstra(int nod)
{
    for( int i = 1; i <= n; ++i )
        d2[i] = INF;
    d2[s] = 0;
    Q.push({s, 0});

    while(!Q.empty())
    {
        nod = Q.top().nod;
        vis[nod] = true;
        for( const auto& e : G[nod] )
        {
            if( !vis[e.nod] && d2[nod] + e.cost < d2[e.nod] )
            {
                d2[e.nod] = e.cost + d2[nod];
                Q.push({e.nod, d2[e.nod]});
            }

        }

        while( !Q.empty() && vis[Q.top().nod])
            Q.pop();
    }
}