Cod sursa(job #2136146)

Utilizator filip.mihalutMihalut Filip filip.mihalut Data 19 februarie 2018 18:15:48
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <bits/stdc++.h>
#define Nmax 50005

using namespace std;

ifstream f ("distante.in");
ofstream g ("distante.out");

int INF = 0x3f3f3f3f;

inline void dijkstra()
{
    vector < pair < int,int > > v[Nmax];
    set < pair < int,int > > heap;
    int d1[Nmax],d[Nmax],n,m,s,i,x,y,c,cc,next_nod,nod;

    f >> n >> m >> s;
    for(i = 1; i <= n; i++)
        f >> d1[i];

    memset(d,INF,sizeof(d));

    for(i = 1; i <= m; i++)
    {
        f >> x >> y >> c;
        v[x].push_back({c,y});
        v[y].push_back({c,x});
    }

    heap.insert({0,s});
    d[s] = 0;

    while(not heap.empty())
    {
        c = heap.begin() -> first;
        nod = heap.begin() -> second;
        heap.erase(heap.begin());
        int l = v[nod].size();
        for(i = 0; i < l; i++)
        {
            cc = v[nod][i].first;
            next_nod = v[nod][i].second;
            if(d[next_nod] > c + cc)
            {
                if(d[next_nod] != INF)
                    heap.erase(heap.find({d[next_nod],next_nod}));
                d[next_nod] = c + cc;
                heap.insert({d[next_nod],next_nod});
            }
        }
    }

    for(i = 1; i <= n; i++)
        if(d1[i] != d[i])
        {
            g << "NU" << '\n';
            return;
        }
    g << "DA" << '\n';
}

int main()
{
    int t;
    f >> t;
    while(t)
    {
        dijkstra();
        t--;
    }
    return 0;
}