Cod sursa(job #2103598)

Utilizator stefanchistefan chiper stefanchi Data 10 ianuarie 2018 15:29:56
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <cstdio>
#include <queue>
#include <vector>
#define NMAX 50010
#define INF  2<<28
using namespace std;
vector <pair <int,int> > graf[NMAX];
queue   <int> q;
int dist_min[NMAX];
int dist[NMAX];
int t;


void bellmanford(int s)
{
    q.push(s);
    while(!q.empty())
    {
        int nod = q.front();
        q.pop();
        for(vector <pair<int,int> > :: iterator it = graf[nod].begin(); it!= graf[nod].end() ; it++)
        {
            if(dist[nod] + it->second < dist[it->first])
            {
                dist[it->first] = dist[nod]+ it->second;
                q.push(it->first);
            }
        }
    }
}

void afisare(int n)
{
    for(int i = 0 ; i < n ; i++)
        if(dist_min[i] != dist[i])
        {
            printf("NU\n");
            return;
        }
    printf("DA\n");
}

void reset()
{
    while(!q.empty()) q.pop();
}

void read()
{
    freopen("distante.in","r",stdin);
    freopen("distante.out","w",stdout);
    scanf("%d",&t);
    int n,m,s,x,y,c;
    for(int i = 0 ; i < t ; i++)
    {
        scanf("%d %d %d",&n,&m,&s);
        for(int j = 1 ; j <= n ; j++)
        {
            scanf("%d",&dist_min[j]);
            graf[j].clear();
            dist[j] = INF;
        }
        for(int j = 0 ; j < m ; j++)
        {
            scanf("%d %d %d",&x,&y,&c);
            graf[x].push_back(make_pair(y,c));
            graf[y].push_back(make_pair(x,c));
        }
        dist[s] = 0;
        bellmanford(s);
        reset();
        afisare(n);
    }
}


int main()
{
    read();
    return 0;
}