Pagini recente » Cod sursa (job #1600475) | Cod sursa (job #1298871) | Cod sursa (job #1787246) | Cod sursa (job #2937355) | Cod sursa (job #2766928)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define maxi 50000
#define INFINITY 0x3f3f3f3f
using namespace std;
ifstream f;
ofstream g;
vector<pair<int,int>> V[1+maxi];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> heap;
int n,m,t,s,D[1+maxi],P[1+maxi];
bool viz[1+maxi];
void INIT()
{
for(int i=1;i<=n;i++)
D[i]=INFINITY,viz[i]=false,P[i]=0;
}
void DIJKSTRA(int x)
{
bool good=true;
viz[x]=true;
D[x]=0;
heap.push(make_pair(0,x));
while(!heap.empty())
{
int sursa=heap.top().second;
viz[sursa]=true;
heap.pop();
for(auto a:V[sursa])
{
int vecin=a.first;
int cost=a.second;
if(!viz[vecin])
if(D[vecin]>D[sursa]+cost)
{
D[vecin]=D[sursa]+cost;
heap.push(make_pair(D[vecin],vecin));
}
}
}
for(int i=1;i<=n;i++)
if(P[i]!=D[i])
good=false;
if(good)
g<<"DA\n";
else g<<"NU\n";
}
void READ()
{
int a,b,c;
f.open("distante.in",ios::in);
g.open("distante.out",ios::out);
f>>t;
for(int l=1;l<=t;l++)
{
f>>n>>m>>s;
INIT();
for(int i=1;i<=n;i++)
f>>P[i];
for(int i=1;i<=m;i++)
{
f>>a>>b>>c;
V[a].push_back(make_pair(b,c));
V[b].push_back(make_pair(a,c));
}
DIJKSTRA(s);
for(int i=1;i<=n;i++)
V[i].clear();
}
f.close();
g.close();
}
int main()
{
READ();
return 0;
}