Pagini recente » Cod sursa (job #2239495) | Cod sursa (job #1468065) | Cod sursa (job #87177) | Cod sursa (job #1902999) | Cod sursa (job #2759324)
///#include <iostream>
#include <fstream>
#include <set>
#include <vector>
const int SIZE = 5e4+100,
INF = 1e8;
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int n, m;
vector <pair<int, int>> v[SIZE];
int d[SIZE];
void readit()
{
int y, x, c;
cin>>n>>m;
for(int i=1; i<=m; i++)
{
cin>>y>>x>>c;
v[y].push_back({x, c});
v[x].push_back({y, c});
}
for(int i=1; i<=n; i++) d[i]=INF;
}
void dijk(int nod)
{
set <pair<int, int>> q;
pair <int, int> now, nxt;
q.insert({0, nod});
while(!q.empty())
{
now=*q.begin();
q.erase(q.begin());
if(d[now.second]==INF)
{
d[now.second]=now.first;
for(int i=0; i<v[now.second].size(); i++)
{
nxt=v[now.second][i];
if(d[nxt.first]>now.first+nxt.second)
q.insert({now.first+nxt.second, nxt.first});
}
}
}
}
int main()
{
readit();
dijk(1);
for(int i=2; i<=n; i++)
if(d[i]!=INF) cout<<d[i]<<' ';
else cout<<"0 ";
return 0;
}