Pagini recente » Cod sursa (job #549027) | Cod sursa (job #1167819) | Cod sursa (job #2497392) | Cod sursa (job #1715958) | Cod sursa (job #2041830)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
int t,n,m,s,a,b,c,aux,cost[50000];
vector < pair <int , int> >v[50000];
pair <int, int > pp, x;
vector <int> sume;
queue <int>q;
int main()
{
fin>>t;
for(int i=1;i<=t;i++)
{
fin>>n>>m>>s;
for(int j=1;j<=n;j++)
{
fin>>aux;
sume.push_back(aux);
}
for(int j=1;j<=m;j++)
{
fin>>a>>b>>c;
v[a].push_back(make_pair(b,c));
v[b].push_back(make_pair(a,c));
}
for(int j=1;j<=n;j++)
cost[j]=1<<30;
q.push(s);
cost[s]=0;
while(!q.empty())
{
aux=q.front();
q.pop();
for(int j=0;j<v[aux].size();j++)
{
x=v[aux][j];
if(cost[x.first]>cost[aux]+x.second)
{
cost[x.first]=cost[aux]+x.second;
q.push(x.first);
}
}
}
bool ok=false;
for(int j=1;j<=n;j++)
{
cout<<cost[j]<<"-"<<sume[j-1]<<endl;
if(cost[j]!=sume[j-1])
ok=true;
}
if(ok==true)
fout<<"NU"<<endl;
else fout<<"DA"<<endl;
/*for(int j=1;j<=n;j++)
{
cout<<j<<"_";
for(int k=0;k<v[j].size();k++)
cout<<v[j][k].first<<" ";
cout<<endl;
}*/
while(sume.size()>=1)
sume.pop_back();
for(int j=1;j<=n;j++)
while(v[j].size()>=1)
v[j].pop_back();
}
return 0;
}