Cod sursa(job #954296)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 28 mai 2013 21:14:07
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include<stdio.h>
#include<fstream>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>

#define x first
#define y second
#define NMAX 50007
#define MaxChar 1000000
#define verf ( (++CharB==MaxChar) ? ( in.read(Buffer,MaxChar),CharB=0 ) : (1) )

using namespace std;

ifstream in("distante.in");

vector < pair < int , int > > v[NMAX];
priority_queue< pair < int , int > > q;
int Dist[NMAX] , dist[NMAX];
int N , M , S , a , b , c , T;
bool ap[NMAX];

long CharB = MaxChar - 1;
char Buffer[MaxChar];

void cit(int &a)
{
    bool ok = 0;
    for( ; !( (Buffer[ CharB ]>='0' && Buffer[ CharB ]<='9') || ( Buffer[ CharB ] == '-' ) ); verf )
        ;
    if ( Buffer[ CharB ] == '-' )
    {
        CharB++;
        ok=1;
    }
    for( a=0; (Buffer[ CharB ]>='0' && Buffer[ CharB ]<='9'); a*=10,a+=( Buffer[ CharB ]-'0'), verf )
        ;
    if(ok)
        a=-a;
}

void dijkstra(int S)
{
    q.push(make_pair(0 , S));
    memset(ap , 0 , sizeof(ap));
    dist[S] = 0;
    while(! q.empty())
    {
        int nod = q.top().y;
        if(ap[nod] == false)
        {
            ap[nod] = true;
            for(int i = 0 ; i < v[nod].size() ; ++ i)
            {
                pair<int , int> val = v[nod][i];
                if(dist[nod] + val.y < dist[val.x])
                {
                    dist[val.x] = val.y + dist[nod];
                    q.push(make_pair(- dist[val.x] , val.x));
                }
            }
        }
        q.pop();
    }
}

int main()
{
    freopen("distante.out" , "w" , stdout);
    cit(T);
    for( ; T > 0 ; -- T)
    {
        cit(N);
        cit(M);
        cit(S);
        for(int i = 1 ; i <= N ; ++ i)
            cit(Dist[i]) , dist[i] = 20000000;
        for(int i = 1 ; i <= N ; ++ i)
            v[i].clear();
        for(int i = 1 ; i <= M ; ++ i)
        {
            cit(a);
            cit(b);
            cit(c);
            v[a].push_back(make_pair(b , c));
            v[b].push_back(make_pair(a , c));
        }

        dijkstra(S);

        int ok = 0;
        for(int i = 1 ; i <= N ; ++ i)
            if(Dist[i] != dist[i])
                ok = 1;
        if(ok == 0)
            printf("DA\n");
        else
            printf("NU\n");
    }
}