Pagini recente » Cod sursa (job #2112607) | Cod sursa (job #2800293) | Cod sursa (job #1044354) | Cod sursa (job #101865) | Cod sursa (job #2924819)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
int teste;
int n, m, x;
priority_queue <pair<int, int> > q;
vector <pair <int, int> > L[50001];
int distante[50001];
void init()
{
int i;
for(i=1; i<=n; i++)
{
distante[i]=99999999;
}
}
void Dijkestra(int grafInceput)
{
int i;
init();
distante[grafInceput]=0;
q.push({0, grafInceput});
while(q.empty()==0)
{
int nodCurent = q.top().second;
int costNodCurent = -q.top().first;
q.pop();
for(auto vecin: L[nodCurent])
{
if(distante[vecin.second]>costNodCurent+vecin.first)
{
distante[vecin.second]=costNodCurent+ vecin.first;
q.push({-distante[vecin.second], vecin.second});
}
}
}
}
int main()
{
///Distante->Info arena
ifstream fin("distante.in");
ofstream fout("distante.out");
int i, j, a, b, v[50001], k, c;
fin >> teste;
for(i=1; i<=teste; i++)
{
fin >> n >> m >> x;
for(k=1; k<=n; k++)
{
fin >> v[k];
L[k].clear();
}
for(j=1; j<=m; j++)
{
fin >> a >> b >> c;
L[a].push_back({c, b});
L[b].push_back({c, a});
}
Dijkestra(x);
bool ok = true;
for(int k = 1; k <= n && ok == true; k++)
if(distante[k] != v[k])
ok = false;
if(ok == false)
fout << "NU\n";
else fout << "DA\n";
}
return 0;
}
/*
2
5 6 1
0 1 7 3 6
1 2 1
1 3 7
1 4 3
3 4 4
2 5 5
4 5 6
4 4 2
1 0 2 3
1 2 1
2 3 1
2 4 1
3 4 1
*/