Cod sursa(job #1746802)

Utilizator KronSabau Valeriu Kron Data 23 august 2016 22:21:47
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <iterator>
#include <climits>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
int n,m,t;
int w1[50001];
int w2[50001];
vector <pair<int,int> > adjList[50001];
int Check()
{
    for(int i=1;i<=n;i++)
        if(w1[i]!=w2[i])
            return 0;
    return 1;
}

void Dijtrak(int n,int t,int start)
{
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
    int top,cost,next,w;
    for(int i=1;i<=n;i++)
        w2[i]=INT_MAX;

    w2[start]=0;
    pq.push(make_pair(0,start));
    while(!pq.empty())
    {
        top=pq.top().second;
        w=pq.top().first;
        pq.pop();
        for(vector<pair<int,int> >::iterator it=adjList[top].begin();it!=adjList[top].end();it++)
        {

            cost=it->first;
            next=it->second;
            if(w2[next]>cost+w)
            {
                w2[next]=cost+w;
                pq.push(make_pair(w2[next],next));
            }
        }
    }


}

int main()
{
    f >> t;
    int start,x,y,cost;

    for(int i=0;i<t;i++)
    {
        f >> n >> m >> start;

        for(int j=1;j<=n;j++)
            f>> w1[j];

        for(int j=0;j<m;j++)
        {
            f  >> x >> y >> cost;
            adjList[x].push_back(make_pair(cost,y));
            adjList[y].push_back(make_pair(cost,x));

        }
       Dijtrak(n,t,start);

        if(Check())
            g<< "DA\n";
            else g << "NU\n";


        for(int i=1;i<=n;i++)
            adjList[i].clear();

    }


    return 0;
}