Pagini recente » Cod sursa (job #3161093) | Cod sursa (job #776342) | Cod sursa (job #799915) | Cod sursa (job #811823) | Cod sursa (job #2603593)
#include <fstream>
#include <vector>
#include <set>
#define cost first
#define ind second
#define inf 0x7fffffff
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int lim=5e4+3;
vector<pair<int,int> > vec[lim];
set<pair<int,int> > s;
int dist[lim];
int main()
{
int n,m,so,x,y,c;
cin>>n>>m; so=1;
for(int i=1;i<=m;++i)
cin>>x>>y>>c,vec[x].push_back({c,y});
s.insert({0,so});
for(int i=1;i<=n;++i)
dist[i]=inf;
dist[so]=0;
while(!s.empty())
{
int c=(*(s.begin())).cost;
int nod=(*(s.begin())).ind;
s.erase(s.begin());
for(auto x:vec[nod])
if(dist[x.ind]>dist[nod]+x.cost)
{
if(dist[x.ind]!=inf) s.erase(s.find({dist[x.ind],x.second}));
dist[x.ind]=dist[nod]+x.cost;
s.insert({dist[x.ind],x.second});
}
}
for(int i=2;i<=n;++i)
if(dist[i]==inf) cout<<0<<' ';
else cout<<dist[i]<<' ';
return 0;
}