Pagini recente » Cod sursa (job #1106830) | Cod sursa (job #2049652) | Cod sursa (job #1332241) | Cod sursa (job #2578683) | Cod sursa (job #2653124)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <string.h>
#define Nmax 50005
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
int j;
int Distanta[Nmax];
bool Vizitat[Nmax];
vector < pair < int, int > > Numbers[Nmax];
vector < int > Numere;
struct comparaElemente
{
bool operator() (int a, int b)
{
return Distanta[a] > Distanta[b];
}
};
priority_queue < int, vector < int >, comparaElemente > Coada;
void Dijkstra(int Nod_Start)
{
int Nod, Vecin, Cost;
Coada.push(Nod_Start);
Vizitat[Nod_Start] = true;
while( !Coada.empty() )
{
Nod = Coada.top();
Coada.pop();
Vizitat[Nod] = false;
for(unsigned int i = 0; i < Numbers[Nod].size(); i ++)
{
Vecin = Numbers[Nod][i].first;
Cost = Numbers[Nod][i].second;
if( Distanta[Nod] + Cost < Distanta[Vecin] )
{
Distanta[Vecin] = Distanta[Nod] + Cost;
if( !Vizitat[Vecin] )
{
Coada.push(Vecin);
Vizitat[Vecin] = true;
}
}
}
}
}
int main()
{
bool ok = true;
int caz;
fin >> caz;
while( caz )
{
int vf, muchii, nodStart;
fin >> vf >> muchii >> nodStart;
for(int i = 1; i <= vf; i ++)
{
int x;
fin >> x;
Numere.push_back(x);
}
for(int i = 1; i <= muchii; i ++)
{
int a, b, cost;
fin >> a >> b >> cost;
Numbers[a].push_back({b, cost});
}
for(int i = 1; i <= vf; i ++)
if( i != nodStart )
Distanta[i] = 1e9;
else
Distanta[i] = 0;
Dijkstra(nodStart);
for(j = 1; j <= vf; j ++)
{
if( j != nodStart && Distanta[j] != 1e9 )
if(Distanta[j] == Numere[j - 1])
continue;
else
{
ok = false;
break;
}
if( j != nodStart && Distanta[j] == 1e9 )
if( Distanta[j] == Numere[j - 1] )
continue;
else
{
ok = false;
break;
}
if( j == nodStart )
if( Distanta[j] == Numere[j - 1] )
continue;
else
{
ok = false;
break;
}
}
if( j - 1 == Numere.size() && ok )
fout << "DA" << "\n";
else
fout << "NU" << "\n";
for(int i = 1; i <= vf; i ++)
Numbers[i].clear();
Numere.clear();
memset(Vizitat, 0, sizeof(Vizitat));
memset(Distanta, 0, sizeof(Distanta));
while( !Coada.empty() )
Coada.pop();
caz --;
}
return 0;
}