Cod sursa(job #2375422)

Utilizator Eduard24Eduard Scaueru Eduard24 Data 8 martie 2019 09:13:55
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <bits/stdc++.h>

using namespace std;

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

struct muchie
{
    int x,c;
};

int t,n,m,s,i,dist[100005],x,y,c,j,ok;
int viz[1000005],d[1000005];
int inf = INT_MAX;

struct comp
{
    bool operator()(int x, int y)
    {
        return d[x]>d[y];
    }
};

vector <muchie> G[100005];
priority_queue<int,vector<int>,comp> pq;

void dijkstra(int S)
{
    viz[S]=1;
    d[S]=0;
    pq.push(S);

    while(!pq.empty())
    {
        int nod = pq.top();
        pq.pop();
        viz[nod]=0;

        for(auto &it : G[nod])
        {
            int nodc = it.x;
            int cost = it.c;

            if(d[nod] + cost < d[nodc])
            {
                d[nodc] = cost + d[nod];
                if(viz[nodc] == 0)
                {
                    pq.push(nodc);
                    viz[nodc]=1;
                }
            }
        }
    }
}

int main()
{
    fin>>t;
    for(i=1;i<=t;i++)
    {
        fin>>n>>m>>s;
        for(j=1;j<=n;j++)
        {
            fin>>dist[j];
            d[j]=inf;
        }
        for(j=1;j<=m;j++)
        {
            fin>>x>>y>>c;
            G[x].push_back({y,c});
            G[y].push_back({x,c});
        }

        dijkstra(s);

        ok=1;
        for(j=1;j<=n;j++)
        {
            if(dist[j] != d[j])
            {
                ok=0;
                break;
            }
        }
        if(ok==1)
        {
            fout<<"DA"<<"\n";
        }
        else
        {
            fout<<"NU"<<"\n";
        }
        for(j=1;j<=n;j++)
        {
            G[j].clear();
        }
    }
    fin.close();
    fout.close();
    return 0;
}