Pagini recente » Cod sursa (job #1651793) | Cod sursa (job #293938) | Cod sursa (job #881359) | Cod sursa (job #2298292) | Cod sursa (job #524663)
Cod sursa(job #524663)
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
const int N=50100;
const int INF=1000000;
int n,m,s;
int d[N],rez[N];
bool inc[N];
vector<int>a[N];
vector<int>c[N];
void read()
{
int x,y,z;
for(int i=1;i<=n;i++)
in>>rez[i];
for(int i=1;i<=m;i++)
{
in>>x>>y>>z;
a[x].push_back(y);
a[y].push_back(x);
c[y].push_back(z);
c[x].push_back(z);
}
}
queue<int>coada;
void reset()
{
while(!coada.empty())
coada.pop();
for(int i=1;i<=n;i++)
{
d[i]=INF,inc[i]=false;
a[i].clear();
c[i].clear();
}
}
void bf()
{
int x,cost,r;
d[s]=0;
coada.push(s);
inc[s] = true;
while(!coada.empty())
{
r=coada.front();
coada.pop();
inc[r] = false;
unsigned int du=a[r].size();
for(int i=0 ; i<du ; i++)
{
x = a[r][i];
cost = c[r][i];
if(d[r]+cost < d[x])
{
d[x] = d[r]+cost;
if(!inc[x])
{
inc[x] = true;
coada.push(x);
}
}
}
}
}
void verify()
{
for(int aa=1;aa<=n;++aa)
if(d[aa]!=rez[aa])
{
out<<"NU\n";
return;
}
out<<"DA\n";
}
void scrie()
{
for(int i=1;i<=n;i++)
out<<d[i]<<"\t";
out<<"\n";
}
int main()
{
int t;
in>>t;
for(int h=1;h<=t;h++)
{
in>>n>>m>>s;
reset();
read();
bf();
//scrie();
verify();
}
return 0;
}