Cod sursa(job #2353987)

Utilizator UnseenMarksmanDavid Catalin UnseenMarksman Data 24 februarie 2019 19:31:08
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("distante.in");
ofstream fout("distante.out");

const int maxn = 5e4+2;
const int inf = 2e9;
int t, n, m, s, cost[maxn], r[maxn];
bool viz[maxn];
vector<pair<int, int> >G[maxn];
struct str{
    int x;
    bool operator <(const str &other)const
    {
        return cost[x]>cost[other.x];
    }
};
priority_queue<str>H;
void dijkstra()
{
    cost[s]=0;
    viz[s]=true;
    H.push({s});
    while(!H.empty())
    {
        str top=H.top();
        H.pop();
        viz[top.x]=false;
        for(auto it:G[top.x])
        {
            if(cost[it.first]>it.second+cost[top.x])
            {
                cost[it.first]=it.second+cost[top.x];
                if(!viz[it.first])
                {
                    viz[it.first]=true;
                    H.push({it.first});
                }
            }
        }
    }
}
bool verif()
{
    for(int i=1; i<=n; i++)
    {
        if(cost[i]!=r[i])
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int x, y, z;
    fin>>t;
    while(t--)
    {
        fin>>n>>m>>s;
        for(int i=1; i<=n; i++)
        {
            fin>>r[i];
            cost[i]=inf;
            G[i].clear();
        }
        for(int i=1; i<=m; i++)
        {
            fin>>x>>y>>z;
            G[x].push_back({y,z});
            G[y].push_back({x,z});
        }
        dijkstra();
        if(verif())
        {
            fout<<"DA\n";
        }
        else
        {
            fout<<"NU\n";
        }
    }
    return 0;
}