Pagini recente » Cod sursa (job #2095414) | Cod sursa (job #1698664) | Cod sursa (job #1152440) | Cod sursa (job #297598) | Cod sursa (job #2432532)
#include <bits/stdc++.h>
#define tip pair<int,int>
#define oo 1000000030
using namespace std;
ifstream inf("dijkstra.in");
ofstream outf("dijkstra.out");
priority_queue<tip> heap;
int n, m;
int cmd[50010];
bool complete[50010];
vector<tip> arc[50010];
int main()
{
inf>>n>>m;
for(int i=1; i<=m; i++)
{
int x, y, z;
inf>>x>>y>>z;
arc[x].push_back({y, z});
arc[y].push_back({x, z});
}
heap.push({0, 1});
for(int i=2; i<=n; i++)
heap.push({-oo, i}),cmd[i]=oo;
cmd[1]=0;
int done=0;
while(done<n)
{
int c, x;
tie(c,x)=heap.top();
heap.pop();
c=-1*c;
if(c>cmd[x]||complete[x])
continue;
complete[x]=true;
done++;
for(auto it:arc[x])
{
int per=it.first;
if(complete[per])
continue;
if(c+it.second<cmd[per])
heap.push({-1*(c+it.second), per}),cmd[per]=c+it.second;
}
}
for(int i=2; i<=n; i++)
{
if(cmd[i]==oo)
outf<<0<<' ';
else
{
outf<<cmd[i]<<' ';
}
}
return 0;
}