Pagini recente » Cod sursa (job #960608) | Cod sursa (job #1892873) | Cod sursa (job #995479) | Cod sursa (job #1370216) | Cod sursa (job #2373957)
#include <bits/stdc++.h>
using namespace std;
const int inf=2e9;
struct edge
{
int nod,cost;
bool operator <(const edge &aux) const
{
return cost>aux.cost;
}
};
vector<edge> v[50010];
int d[50010];
priority_queue<edge> q;
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
int n,m,x,y,c;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&c);
v[x].push_back({y,c});
}
for(int i=1;i<=n;i++) d[i]=inf;
d[1]=0;
q.push({1,0});
while(!q.empty())
{
int nod=q.top().nod,c=q.top().cost;
q.pop();
if(c>d[nod]) continue;
for(int i=0;i<v[nod].size();i++)
{
int vec=v[nod][i].nod,cost=v[nod][i].cost;
if(c+cost<d[vec])
{
d[vec]=c+cost;
q.push({vec,d[vec]});
}
}
}
for(int i=2;i<=n;i++) printf("%d ",d[i]);
return 0;
}