Pagini recente » Cod sursa (job #1550286) | Cod sursa (job #1316602) | Cod sursa (job #1930465) | Cod sursa (job #2272338) | Cod sursa (job #2424174)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
#define NMAX 50005
#define inf 1e9
vector<pair<int,int> > graph[NMAX];
priority_queue<pair<int, int> >myheap;
int distanta[NMAX], viz[NMAX], n, m, rezultat[NMAX], nr, s;
int verif()
{
for(int i = 1; i <= n; i ++)
if(distanta[i] != rezultat[i])
return 0;
return 1;
}
void dijkstra(int s)
{
for(int i = 1; i <= n; i ++)
{
distanta[i] = inf;
viz[i] = 0;
}
distanta[s] = 0;
viz[s] = 0;
myheap.push({0,s});
int l = graph[s].size();
for(int i = 0; i < l; i ++)
{
//int cost = graph[1][i].second;
int vecin = graph[s][i].first;
myheap.push({-inf,vecin});
}
while(!myheap.empty())
{
pair<int,int> best = myheap.top();
myheap.pop();
int index = best.second;
if(viz[index] == 0)
{
viz[index] = 1;
int l = graph[index].size();
for(int i = 0; i < l; i ++)
{
int cost = graph[index][i].second;
int vecin = graph[index][i].first;
if(distanta[index] + cost < distanta[vecin])
{
distanta[vecin] = distanta[index] + cost;
myheap.push({-distanta[vecin],vecin});
}
}
}
}
}
void citire()
{
f>>nr;
for(int k = 1; k<= nr; k++)
{
f>>n>>m>>s;
for(int i = 1; i <= n; i ++)
f>>rezultat[i];
for(int i = 0; i < m; i ++)
{
int a, b, c;
f>>a>>b>>c;
graph[a].push_back({b,c});
graph[b].push_back({a,c});
}
dijkstra(s);
if(verif() == 1)
g<<"DA\n";
else g<<"NU\n";
for(int i = 1; i<=n; i ++)
graph[i].clear();
}
}
int main()
{
citire();
return 0;
}