Pagini recente » Cod sursa (job #1332405) | Cod sursa (job #1070611) | Cod sursa (job #626672) | Cod sursa (job #609722) | Cod sursa (job #2723088)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int lim=5e4+5;
const int inf=1e9+7;
vector<pair<int,int> > vec[lim];
set<pair<int,int> > s;
int dist[lim];
int main()
{
int n,m,a,b,c;
in>>n>>m;
for(int i=1;i<=m;++i)
{
in>>a>>b>>c;
vec[a].push_back({b,c});
}
for(int i=1;i<=n;++i)
dist[i]=inf;
dist[1]=0;
s.insert({0,1});
while(!s.empty())
{
int bst=(*s.begin()).first;
int nod=(*s.begin()).second;
s.erase(s.begin());
for(auto p:vec[nod])
if(dist[p.first]>bst+p.second)
{
if(dist[p.first]!=inf)
s.erase(s.find({dist[p.first],p.first}));
dist[p.first]=bst+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;
}