Cod sursa(job #1631373)

Utilizator alexb97Alexandru Buhai alexb97 Data 5 martie 2016 15:22:50
Problema Distante Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;

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

struct Edge
{
    int node, cost;
    const bool operator < (const Edge other) const
    {
        return cost > other.cost;
    }
};

int n, t, m, s;
//vector<vector<pair<int, int> > > g;
//vector<int> d;
//vector<int> verif;
priority_queue<Edge> Q;
int d[50005], verif[50005];

int main()
{
    is >> t;
    int citire1;
    int v1, v2, c3;
    int x;
    bool ok = true;
    for(int i = 1; i <= t; ++i)
    {
        is >> n >> m >> s;
        vector<vector<pair<int, int> > > g;
        g = vector<vector<pair < int, int> > >(n+1);
       // d = vector<int>(n+1);
        //verif = vector<int>(n+1, 0);
        //verif.resize(n+1);
        memset(verif, 0, sizeof verif);
        memset(d, INF, sizeof d);
        for(int j = 1; j <= n; ++j)
        {
            is >> citire1;
            verif[j] = citire1;
        }
        /*if(verif[s] != 0)
        {
            os << "NU" << '\n';
            continue;
        }
        */
        d[s] = 0;
        for(int k = 1; k <= m;++k)
        {
            is >> v1 >> v2 >> c3;
            g[v1].push_back({v2, c3});
            g[v2].push_back({v1, c3});
        }
        Q.push({s, d[s]});
        while(!Q.empty())
        {
            x = Q.top().node;
            Q.pop();
            for(const auto & y: g[x])
            {
                if(d[y.first] > d[x] + y.second)
                {
                    d[y.first] = d[x] + y.second;
                    Q.push({y.first, d[y.first]});
                }
            }
        }


        for(int j = 1; j <= n; ++j)
        {
            if(d[j] != verif[j])
            {
                ok = false;
            }
        }
        if(ok)
            os << "DA" << '\n';
        else
            os << "NU" << '\n';
        //g.clear();
        //verif.clear();
        //d.clear();
        while(!Q.empty())
        {
            Q.pop();
        }
    }
    is.close();
    os.close();
    return 0;
}