Pagini recente » Cod sursa (job #1138364) | Cod sursa (job #1433550) | Istoria paginii runda/max_min/clasament | Cod sursa (job #1126991) | Cod sursa (job #2500104)
#include <bits/stdc++.h>
#define NMAX 100005
#define INF INT_MAX
#define cin fin
#define cout fout
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, p;
int x, y, cost;
long long d[NMAX];
vector< pair<int, int> > a[NMAX]; //nod, cost
queue< int > coada;
void read();
void BFS();
int main()
{
read();
for(int i=1; i<=n; ++i)
d[i]=INF;
coada.push(p);
d[p]=0;
BFS();
for(int i=1; i<=n; ++i)
{
if(d[i]==INF) cout<<0<<' ';
else cout<<d[i]<<' ';
}
return 0;
}
void BFS()
{
while(!coada.empty())
{
int first=coada.front();
coada.pop();
for(int i=0; i<a[first].size(); ++i)
{
int nod=a[first][i].first;
int c=a[first][i].second;
if(d[nod]>d[first]+c)
{
d[nod]=d[first]+c;
coada.push(nod);
}
}
}
}
void read()
{
cin>>n>>m>>p;
for(int i=1; i<=m; ++i)
{
cin>>x>>y>>cost;
a[x].push_back({y, cost});
}
}