Cod sursa(job #2202253)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 8 mai 2018 09:45:42
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <bits/stdc++.h>
#define N_MAX 50005
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
int n,m,s,v[N_MAX],boy[N_MAX];
bool inqueue[N_MAX];
const int inf = 1<<30;
vector <pair <int, int> > lista[N_MAX];
struct compare{
    bool operator() (int d1, int d2)
    {
        return v[d1]>v[d2];
    }
};
void citire()
{
    for(int i=1; i<=n; i++)
    {
        while(!lista[i].empty())
        {
            lista[i].pop_back();
        }
    }
    in >> n >> m >> s;
    for(int i=1; i<=n; i++)
    {
        in >> boy[i];
    }
    for(int i=0; i<m; i++)
    {
        int x,y,z;
        in >> x >> y >> z;
        lista[x].push_back(make_pair(y,z));
        lista[y].push_back(make_pair(x,z));
    }
}
priority_queue <int, vector<int>, compare> pq;
void dijkstra(int node)
{
    for(int i=1; i<=n; i++)
    {
        v[i]=inf;
    }
    v[node]=0;
    inqueue[node]=true;
    pq.push(node);
    while(!pq.empty())
    {
        int nod=pq.top();
        inqueue[nod]=false;
        pq.pop();
        for(int i=0; i<lista[nod].size(); i++)
        {
            int vecin=lista[nod][i].first;
            int cost=lista[nod][i].second;
            if(v[nod]+cost<v[vecin])
            {
                v[vecin]=v[nod]+cost;
                if(!inqueue[vecin])
                {
                    inqueue[vecin]=true;
                    pq.push(vecin);
                }
            }
        }
    }
}
bool afis()
{
    for(int i=1; i<=n; i++)
    {
        if(v[i]!=boy[i] || (v[i]==inf && boy[i]==0))
            return false;
    }
    return true;
}
int main()
{
    int o;
    in >> o;
    for(int i=1; i<=o; i++)
    {
        citire();
        dijkstra(s);
        if(afis())
            out << "DA\n";
        else
            out << "NU\n";
    }
    return 0;
}