Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Diferente pentru preoni-2008/runda-1/5-8 intre reviziile 10 si 9 | Istoria paginii utilizator/litaandrei | Cod sursa (job #2116730)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
#define lim 50010
#define inf 2e9
int n, m;
int path[lim],nod;
bool viz[lim];
vector <pair <int, int>> G[lim];
priority_queue <pair <int, int>> q;
int main()
{
int x,y,c;
fin>>n>>m;
for (int i=1; i<=n; i++)
{
fin>>x>>y>>c;
G[x].push_back({y,c});
G[y].push_back({x,c});
}
for (int i=2; i<=n; i++) path[i]=inf;
q.push({0,1});
while (!q.empty())
{
nod=q.top().second;
if (viz[nod]) continue;
viz[nod]=1;
path[nod]=-q.top().first;
q.pop();
for (auto it:G[nod])
if (path[it.first] > path[nod] + it.second)
{
path[it.first] = path[nod] + it.second;
q.push({-path[it.first], it.first});
}
}
for (int i=2; i<=n; i++)
fout<<path[i]<<' ';
fout.close();
return 0;
}