Pagini recente » Cod sursa (job #820514) | Cod sursa (job #2036765) | Cod sursa (job #2420730) | Cod sursa (job #1819635) | Cod sursa (job #2130596)
#include <bits/stdc++.h>
using namespace std;
#define nmax 100003
const int INF = 0x3f3f3f3f;
int n,m,p,from,to,cost;
int dist[nmax];
bool viz[nmax];
vector < pair < int, int > > matrice[nmax];
priority_queue < pair < int, int > > heap;
void Dijkstra(int nod)
{
heap.push({0,1});
dist[nod]=0;
while(!heap.empty())
{
nod=heap.top().second;
heap.pop();
if(viz[nod]) continue;
viz[nod]=1;
for(vector < pair < int, int > >::iterator it=matrice[nod].begin(); it!=matrice[nod].end(); it++)
if(dist[it->first]>dist[nod]+it->second)
{
dist[it->first]=dist[nod]+it->second;
heap.push({-dist[it->first],it->first});
}
}
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&from,&to,&cost);
matrice[from].push_back({to,cost});
}
memset(dist,INF,sizeof dist);
Dijkstra(1);
for(int i=2; i<=n; i++)
if(dist[i]!=INF) printf("%d ",dist[i]);
else printf("0 ");
return 0;
}