Cod sursa(job #1092041)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 26 ianuarie 2014 14:57:43
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.5 kb
#include <cstdio>
#include <vector>
#include <queue>
#define Nmax 50099
#define Mmax 100099
#define oo 2000000000
using namespace std;


int T,N,M,S,d[Nmax],v[Nmax];

struct muchie{int nod,c;};
vector < muchie > Graph[Nmax];

struct cmp
{
    bool operator()(const int &a,const int &b)
    {
        return (d[a]>d[b]);
    };
};
priority_queue < int , vector < int > , cmp > pq;

int Dijkstra(const int &S)
{
    for(int i=1;i<=N;++i)d[i]=oo;
    d[S]=0;
    pq.push(S);
    for( ; !pq.empty() ; pq.pop())
    {
        int x=pq.top();
        for(vector<muchie>::iterator it=Graph[x].begin();it!=Graph[x].end();++it)
            if(d[x]+it->c < d[it->nod])
            {
                d[it->nod]=d[x]+it->c ;
                pq.push(it->nod);
            }
    }
    for(int i=1;i<=N;++i)
        if(d[i]==oo && v[i])return 0;
        else if(d[i]!=v[i])return 0;

    return 1;
}

void ReadInput()
{
    char tmp[300099];
    fgets(tmp, 300000, stdin);//printf("%s\n",tmp);
    char *p=tmp;
    N=M=S=0;
    for( ;'0'<=*p && '9'>=*p;++p)N=N*10+(*p-'0');
    for (; '0' > *p || *p > '9'; p++);
    for( ;'0'<=*p && '9'>=*p;++p)M=M*10+(*p-'0');
    for (; '0' > *p || *p > '9'; p++);
    for( ;'0'<=*p && '9'>=*p;++p)S=S*10+(*p-'0');
    for (; '0' > *p || *p > '9'; p++);
    fgets(tmp, 300000, stdin);//printf("%s\n",tmp);
    p=tmp;
    for(int i=1;i<=N;++i)
    {
        v[i]=0;
        for( ;'0'<=*p && '9'>=*p;++p)v[i]=v[i]*10+(*p-'0');
        for (; '0' > *p || *p > '9'; p++);
    }
    for(int i=1;i<=M;++i)
    {
        int x=0,y=0,c=0;
        char tmp[109];
        fgets(tmp,100, stdin);//printf("%s\n",tmp);
        char *p=tmp;
        for( ;'0'<=*p && '9'>=*p;++p)x=x*10+(*p-'0');
        for (; '0' > *p || *p > '9'; p++);
        for( ;'0'<=*p && '9'>=*p;++p)y=y*10+(*p-'0');
        for (; '0' > *p || *p > '9'; p++);
        for( ;'0'<=*p && '9'>=*p;++p)c=c*10+(*p-'0');
        muchie aux;
        aux.nod=x,aux.c=c; Graph[y].push_back(aux);
        aux.nod=y,aux.c=c; Graph[x].push_back(aux);
    }
}

int main()
{
    freopen("distante.in","r",stdin);
    freopen("distante.out","w",stdout);
    char tmp[300099];
    fgets(tmp, 300000, stdin);//printf("%s\n",tmp);
    char *p=tmp;
    T=0;
    for( ;'0'<=*p && '9'>=*p;++p)T=T*10+(*p-'0');
    for(int i=1;i<=T;++i)
    {
        ReadInput();
        if(Dijkstra(S))printf("DA\n");
        else printf("NU\n");
        for(int j=1;j<=N;++j)Graph[j].clear();
    }
    return 0;
}