Cod sursa(job #1130135)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 28 februarie 2014 11:28:59
Problema Distante Scor 100
Compilator cpp Status done
Runda preoji2014_3_11_12 Marime 2.28 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <memory.h>
#include <queue>
#include <bitset>

using namespace std;

#define next second
#define cost first

#define DIM 666013
#define Nmax 50005
#define INF 0x3f3f3f3f

char buff[DIM];
int poz = DIM-1;
int T;
void scanF(int &numar)
{
     numar = 0;
     while (buff[poz] < '0' || buff[poz] > '9')
          if (++poz == DIM)fread(buff,1,DIM,stdin),poz=0;
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
}

int Dlor[Nmax],D[Nmax],N,M,S;
vector<pair<int,int> > G[Nmax];
queue<int> Q;
bitset<Nmax> inQ;

void bellman_ford(int k)
{
    Q.push(k);
    inQ[k] = 1;
    D[k] = 0;
    while(!Q.empty())
    {
        k = Q.front();Q.pop();
        inQ[k] = 0;
        for(vector<pair<int,int> >::iterator it = G[k].begin(); it != G[k].end(); ++it)
            if(D[it->next] > D[k] + it->cost)
            {
                D[it->next] = D[k] + it->cost;
                if(D[it->next] < Dlor[it->next])///tzapa mare :))
                {
                    printf("NU\n");
                    return;
                }
                if(!inQ[it->next])
                {
                    Q.push(it->next);
                    inQ[it->next] = 1;
                }
            }
    }
    for(int i = 1; i <= N; ++i)
        if(D[i] != Dlor[i]) /// tzapa pe dos
        {
            printf("NU\n");
            return;
        }
    printf("DA\n");
}

void resetus()
{
    for(int i = 1; i <= N; ++i)
        G[i].clear();
    inQ = 0;
    while(!Q.empty())Q.pop();
}

int main()
{
    freopen("distante.in","r",stdin);
    freopen("distante.out","w",stdout);

    scanF(T);
    int a,b,c;
    while(T--)
    {

        scanF(N);scanF(M);scanF(S);
        for(int i = 1; i <= N; ++i)
            scanF(Dlor[i]);
        for(int i = 1; i <= M; ++i){
            scanF(a);
            scanF(b);
            scanF(c);
            G[a].push_back(make_pair(c,b));
            G[b].push_back(make_pair(c,a));
        }
        memset(D,INF,sizeof(D));
        bellman_ford(S);
        resetus();
    }


    return 0;
}