Cod sursa(job #1525792)

Utilizator kassay_akosKassay Akos kassay_akos Data 15 noiembrie 2015 16:34:11
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.37 kb
#include <iostream>
#include <vector>                   // to use the vector
#include <utility>                  // to use the pair <int,int>
#include <fstream>                  // to open files
#include <queue>                    // to use queues
#include <algorithm>
#include <cstring>                  // for the memset
#include <string.h>
using namespace std;

const int nmax=50005;
const int Inf = 1 << 30;

struct NOD {
    int b,cost ;
    NOD *next ;
};

int n,m,s;

//vector <pair <int,int> > v[nmax];
NOD *v[nmax];
int d[nmax], dis[nmax];                         // d distans calculated by me and dis the given distans to compare it its ok
bool InQueue[nmax];                             // is a number already in the queue
queue<int> Q;                                   // number in the queue

void readData() ;

int main()
{
    int T;
    freopen("distante.in","r",stdin);
    freopen("distante.out","w",stdout);
    scanf("%d",&T);

    for (int test = 0;test < T ; test++) {
        readData();

        InQueue[s] = true ;
        Q.push(s);
        d[s]=0;
        while (!Q.empty()) {
            int r = Q.front();
            Q.pop();
            NOD *q = v[r];
            while(q) {
                if (d[r] + q->cost < d[q->b] ) {
                    d[q->b] = d[r] + q->cost;
                    if (!InQueue[q->b]) {
                        InQueue[q->b] = true;
                        Q.push(q->b) ;
                    }
                }
                q= q->next ;
            }
            InQueue[r] = false;
        }
        bool equalDtoDim = true;
        for(int i = 1; i<=n; i++)
            if (dis[i]!=d[i])
                if (dis[i]!=0 || d[i]!=Inf) equalDtoDim=false;
        if (equalDtoDim) printf("DA\n");
        else printf("NU\n");
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}

void readData() {
    scanf("%d %d %d",&n, &m, &s);

    //memset(d, Inf, sizeof(d));
    //memset(InQueue, false, sizeof(InQueue));

    for(int i = 1; i<n+1; i++) {
        scanf("%d",&dis[i]);
        d[i]= Inf;
        InQueue[i]=false;
    }
    int a,b,c;
    for( int i = 0; i<m; i++) {
        scanf("%d %d %d",&a, &b, &c);
        //v[a].push_back(make_pair(b,c));
        NOD *w = new NOD;
        w->b = b;
        w->cost = c;
        w->next = v[a];
        v[a]=w;
    }
}