Pagini recente » Cod sursa (job #1562451) | Cod sursa (job #1038765) | Cod sursa (job #1458396) | Cod sursa (job #518302) | Cod sursa (job #2670670)
#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define NMAX 250005
using namespace std;
FILE *fin = fopen("dijkstra.in" , "r");
FILE *fout = fopen("dijkstra.out" , "w");
int n , m , d[NMAX];
bool uz[NMAX];
vector < pair <int , int> > G[NMAX];
vector < pair <int , int> > :: iterator it;
inline bool compar(int x , int y)
{
return (d[x] > d[y]);
}
priority_queue < int , vector <int> , bool (*)(int , int) > q(compar);
void dijkstra()
{
int curent , lg;
for(int i = 1 ; i <= n ; i ++) d[i] = 1e9;
d[1] = 0;
uz[1] = 1;
q.push(1);
while(!q.empty())
{
curent = q.top();
q.pop();
uz[curent] = 0;
for(int i = 0 ; i < G[curent].size() ; i ++)
{
int nod = G[curent][i].first;
int cost = G[curent][i].second;
lg = cost + d[curent];
if(d[nod] > lg)
{
d[nod] = lg;
if(uz[nod] == 0)
{
uz[nod] = 1;
q.push(nod);
}
}
}
}
}
int main()
{
fscanf(fin , "%d%d" , &n , &m);
for(int i = 1 ; i <= m ; i ++)
{
int x , y , cost;
fscanf(fin , "%d%d%d" , &x , &y , &cost);
G[x].push_back({y , cost});
}
dijkstra();
for(int i = 1 ; i <= n ; i ++)
if(d[i] == 1e9)
d[i] = 0;
for(int i = 2 ; i <= n ; i ++)
fprintf(fout , "%d " , d[i]);
return 0;
}