Cod sursa(job #1405152)

Utilizator Mr.DoomRaul Ignatus Mr.Doom Data 28 martie 2015 21:53:07
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;

#define INF 0x3f3f3f3f

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

struct Edge{
    int y, cost;
    bool operator < ( const Edge& c ) const
    {
        return c.cost < cost;
    }
};

int n, m, s;
bool yep;
int t;
vector<vector<Edge> > G;
vector<int> D1, D2;
priority_queue<Edge> Q;
bitset<50001> v;

void Dijkstra(int node);

int main()
{
    is >> t;
    while ( t-- )
    {
        is >> n >> m >> s;
        G = vector<vector<Edge>>(n + 1);
        D1 = vector<int>(n + 1, INF);
        D2 = vector<int>(n + 1);
        v.reset();
        for ( int i = 1; i <= n; ++i )
            is >> D2[i];
        int x, y, cost;
        for ( int i = 1; i <= m; ++i )
        {
            is >> x >> y >> cost;
            G[x].push_back({y, cost});
            G[y].push_back({x, cost});
        }
        Dijkstra(s);
        yep = false;
        for ( int i = 1; i <= n; ++i )
            if ( D1[i] != D2[i] )
            {
                os << "NU" << '\n';
                yep = true;
                break;
            }
        if ( !yep )
            os << "DA" << '\n';
    }
    is.close();
    os.close();
    return 0;
}

void Dijkstra(int node)
{
    int x, xcost;
    D1[node] = 0;
    Q.push({node, 0});
    while ( !Q.empty() )
    {
        x = Q.top().y;
        xcost = Q.top().cost;
        Q.pop();
        if ( v[x] ) continue;
        v[x] = 1;
        for ( const auto& g : G[x] )
            if ( D1[g.y] > xcost + g.cost )
            {
                D1[g.y] = xcost + g.cost;
                Q.push({g.y, D1[g.y]});
            }

        while ( !Q.empty() && v[Q.top().y] )
            Q.pop();
    }
}