Pagini recente » Cod sursa (job #1147553) | Cod sursa (job #1356466)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream is("distante.in");
ofstream os("distante.out");
const int MAXN = 50001, INF = 10001;
struct Edge{
int nod, cost;
const bool operator < (const Edge& e) const
{
return cost > e.cost;
}
};
vector<vector<Edge> > G;
int t, n, m, s;
int d1[MAXN];
string str;
int d2[MAXN];
bool vis[MAXN];
priority_queue<Edge> Q;
bool ok;
void Dijkstra(int nod);
int main()
{
is >> t;
while(t)
{
ok = true;
is >> n >> m >> s;
G.resize(n+1);
for( int i = 1; i <= n; ++i )
is >> d1[i];
int a, b, c;
for( int i = 1; i <= m; ++i )
{
is >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
Dijkstra(s);
for( int i = 1; i <= n; ++i )
if( d1[i] != d2[i] )
ok = false;
if( ok == true )
str ="DA";
else
str ="NU";
os << str << '\n';
--t;
}
is.close();
os.close();
return 0;
}
void Dijkstra(int nod)
{
for( int i = 1; i <= n; ++i )
d2[i] = INF;
d2[s] = 0;
Q.push({s, 0});
while(!Q.empty())
{
nod = Q.top().nod;
vis[nod] = true;
for( const auto& e : G[nod] )
{
if( !vis[e.nod] && d2[nod] + e.cost < d2[e.nod] )
{
d2[e.nod] = e.cost + d2[nod];
Q.push({e.nod, d2[e.nod]});
}
}
while( !Q.empty() && vis[Q.top().nod])
Q.pop();
}
}