Cod sursa(job #1736765)

Utilizator stefanchistefan chiper stefanchi Data 2 august 2016 16:33:12
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#define N 50010
#define inf 1<<30
using namespace std;
vector <pair <int,int > > varf[N];
priority_queue <pair <int,int> > cost;
int dist[N];
int solutie[N];
int t,n,m,s;

void dijkstra()
{
    int ok = 1;
    while(!cost.empty())
    {
        int vf_b = cost.top().second;
        int drum = -cost.top().first;
        cost.pop();
        for(vector <pair <int,int > > :: iterator it = varf[vf_b].begin() ; it != varf[vf_b].end() ; ++it)
        {
            if(dist[it->first] > drum + it ->second)
            {
                dist[it->first] = drum + it->second;
                cost.push(make_pair(-dist[it->first],it->first));
            }
        }
    }
    for(int i = 1 ; i <= n ; i++)
    {
        if(dist[i]!= solutie[i])
        {
            ok = 0;
            printf("NU\n");
            break;
        }
    }
    if(ok == 1)
        printf("DA\n");
   while(!cost.empty())
    cost.pop();
}


void read()
{
    freopen("distante.in","r",stdin);
    freopen("distante.out","w",stdout);
    scanf("%d",&t);
    int a,b,c;
    for(int i = 0 ; i < t ; i++)
    {
        scanf("%d %d %d",&n,&m,&s);
        for(int j = 1 ; j <= n ; j++)
            dist[j] = inf;
        dist[s] = 0;
        for(int j = 1 ; j <= n ; j++)
            scanf("%d ",&solutie[j]);
        for(int j = 1 ; j <= m ;j++)
        {scanf("%d %d %d",&a,&b,&c);
         varf[a].push_back(make_pair(b,c));
        }
        cost.push(make_pair(0,s));
        dijkstra();
    }
}
int main()
{
    read();
    return 0;
}