Pagini recente » Cod sursa (job #2716243) | Cod sursa (job #2730793) | Cod sursa (job #1842447) | Cod sursa (job #560463) | Cod sursa (job #1914273)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector < pair<int,int> > a[50005];
priority_queue < pair<int,int> > h;
int n,m;
int x,y,c;
int d[50005];
bool viz[50005],prel[50005];
int main()
{
fin>>n>>m;
for (int i=0;i<m;i++) {
fin>>x>>y>>c;
a[x].push_back(make_pair(y,c));
}
d[1]=0;
for (int i = 1; i <= n; i++) {
d[i] = 1000000000;
}
d[1] = 0;
h.push(make_pair(0,1));
while (!h.empty()) {
do {
x=h.top().second;
h.pop();
}while (!h.empty() && prel[x]);
prel[x]=true;
for (int i=0;i<a[x].size();i++) {
y=a[x][i].first;
if (!prel[y]) {
if (d[x]+a[x][i].second<d[y])
d[y]=d[x]+a[x][i].second;
h.push(make_pair(-d[y],y));
}
}
}
for (int i=2;i<=n;i++) {
if (d[i]==1000000000) fout<<0<<' ';
else fout<<d[i]<<' ';
}
}