Pagini recente » Cod sursa (job #2243299) | Cod sursa (job #455299) | Cod sursa (job #550517) | Cod sursa (job #2253195) | Cod sursa (job #2785905)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int inf=2e9+7;
const int lim=5e4+5;
vector<pair<int,int> > vec[lim];
set<pair<int,int> > s;
int dist[lim];
int n,m,x,y,c;
int main()
{
in>>n>>m;
dist[1]=0;
for(int i=2;i<=n;++i)
dist[i]=inf;
for(int i=1;i<=m;++i)
in>>x>>y>>c,
vec[x].emplace_back(y,c);
s.insert({0,1});
while(!s.empty())
{
int c=(*(s.begin())).first;
int x=(*(s.begin())).second;
s.erase(s.begin());
for(auto p:vec[x])
if(dist[p.first]>dist[x]+p.second)
{
if(dist[p.first]!=inf)
s.erase(s.find({dist[p.first],p.first}));
dist[p.first]=dist[x]+p.second;
s.insert({dist[p.first],p.first});
}
}
for(int i=2;i<=n;++i)
if(dist[i]==inf) out<<0<<' ';
else out<<dist[i]<<' ';
return 0;
}