Cod sursa(job #759387)

Utilizator Theorytheo .c Theory Data 17 iunie 2012 20:06:56
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.12 kb
#include<fstream>
#include<vector>
#include<cstring>
#include<queue>

#define nmax 50020
#define oo 10000000
using namespace std;

ifstream fin("distante.in");
ofstream fout("distante.out");

bool s[nmax];//vizitarea nodurilor intermediare
int d[nmax], n, m  ;//distnta minima de la nodul r la nodul indice
int x,y,dis;
int l[nmax];
struct
Nod{
    int x, cost;
    Nod(int _x, int _cost)
    {
      x = _x;
      cost = _cost;
    }
    Nod()
    {
        x = 0; cost = 0;
    }
    bool operator < (const Nod &x) const{
        return cost > x.cost;
    }
} ;
vector <Nod> T[nmax]; //graful

void dijkstra(int r)
{
   int i , c, y ,x;
   for(int  i = 1; i <= n; i++)
        d[i] = oo, s[i] = false;

    d[r] = 0;
     // fout<<n <<'\n';
    priority_queue <Nod> H;

    H.push(Nod(r,d[r]));

    while(!H.empty())
    {
        x = H.top().x;
        H.pop();
        if(s[x])
            continue;
        s[x] = true;
        for(vector <Nod> :: iterator it = T[x].begin(); it != T[x].end(); it++)
        {
            y = it -> x;
            c = it -> cost + d[x];
            if(c < d[y])
            {
                d[y] = c;
                H.push(Nod (y, d[y]) );
            }
        }
    }

    int ver = 0 ;
    for(int i = 1; i <= n;i++)
        if(d[i] == oo)
            d[i] = 0 ;

//    for(int i = 1; i <= n ; i++)
//            fout<<d[i] << " " ;

    for(int i = 1; i <= n;i++)
        if(d[i] != l[i] )
             ver = 1;

    if(!ver)
    fout <<"DA" <<'\n';
    else
    fout <<"NU" <<'\n';


}
void read()
{
    int nr ;
    fin >>nr;
    while(nr)
    {
        int sursa;
        --nr;
        fin >> n >> m >> sursa;

       for(int i = 1; i <= n; i++)
            T[i].clear();


        for(int i = 1; i <= n; i++)
            fin>>l[i];
        for(int i = 1; i <= m ;i++)
        {
            fin >>x >> y >>dis;
            T[x].push_back(Nod(y,dis));
            T[y].push_back(Nod(x,dis));

        }
        dijkstra(sursa);



    }



}
int main()
{
    read();

    fin.close();
    return 0;;

}