Pagini recente » Cod sursa (job #1172917) | Cod sursa (job #889452) | Cod sursa (job #2207027) | Cod sursa (job #2771929) | Cod sursa (job #536164)
Cod sursa(job #536164)
// http://infoarena.ro/problema/distante
#include <fstream>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
int nrGraphs;
int nodes,edges,startNode;
int dist[50001];
bool canBeAchieved[50001];
void solve();
int main() {
in >> nrGraphs;
for(int step=1;step<=nrGraphs;step++) {
in >> nodes >> edges >> startNode;
solve();
}
in.close();
out.close();
return (0);
}
void solve() {
int from,to,cost;
bool ok = true;
for(int i=1;i<=nodes;i++)
in >> dist[i];
if(dist[startNode])
ok = false;
if(ok)
for(int i=1;i<=edges;i++) {
in >> from >> to >> cost;
// verificam daca distantele se pot imbunatati
if(dist[from] + cost < dist[to] && dist[to] + cost < dist[from]) {
ok = false;
break;
}
// daca distantele sunt realizabile
if(dist[from] + cost == dist[to])
canBeAchieved[to] = true;
if(dist[to] + cost == dist[from])
canBeAchieved[from] = true;
}
else
// datele tot trebuie citite, chiar daca nu se efectueaza operatiile
for(int i=1;i<=edges;i++)
in >> from >> to >> cost;
if(ok)
// verificam daca distantele sunt realizabile
for(int currentNode=1;currentNode<=nodes;currentNode++)
if(currentNode != startNode)
if(!canBeAchieved[currentNode]) {
ok = false;
break;
}
memset(canBeAchieved,false,nodes+1);
if(ok)
out << "DA\n";
else
out << "NU\n";
}