Pagini recente » Istoria paginii runda/pregatire_oji_11-12_ | Cod sursa (job #1807742) | Cod sursa (job #1870313) | Cod sursa (job #2616802) | Cod sursa (job #1894842)
#include <iostream>
#include <queue>
#include <vector>
#include <fstream>
#define nmax 50005
#define inf 100000
using namespace std;
priority_queue<pair<int,int> > Q;
vector <pair< int, int > > G[nmax];
int dist[nmax];
void dijkstra(pair<int,int> start){
Q.push(start);
int nod,cost,i;
while(!Q.empty()){
nod=Q.top().second;
cost=Q.top().first;
Q.pop();
for(i=0;i<G[nod].size();i++){
if(dist[G[nod][i].second]>-cost+G[nod][i].first) {
dist[G[nod][i].second]=-cost+G[nod][i].first;
Q.push(make_pair(-dist[G[nod][i].second],G[nod][i].second));
}
}
}
}
int main()
{
int v[nmax];
ifstream f("distante.in");
ofstream g("distante.out");
int t,k,n,m,s,i,x,y,c;
bool sem;
f >> t;
for(k=1;k<=t;k++){
f >> n >> m >> s;
for(i=1;i<=n;i++){
f >> v[i];
}
for(i=1;i<=n;i++){
dist[i]=inf;
}
for(i=1;i<=m;i++){
f >> x >> y >> c;
G[x].push_back(make_pair(c,y));
G[y].push_back(make_pair(c,x));
}
dist[s]=0;
pair <int,int > start;
start=make_pair(0,s);
dijkstra(start);
sem=true;
for(i=1;i<=n;i++ && sem){
if (v[i]!=dist[i]) sem=false;
}
if (sem) g << "DA\n";
else g << "NU\n";
}
}