Pagini recente » Cod sursa (job #2767895) | Cod sursa (job #1319429) | Cod sursa (job #1318455) | Cod sursa (job #1111178) | Cod sursa (job #2172149)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
#define N_MAX 50005
#define inf (1<<31) - 1
int n, m, dist[N_MAX], viz[N_MAX];
vector <pair <int, int> > G[N_MAX];
/// dist nod
priority_queue <pair <int, int> > pq;
void dijkstra(int start)
{
int nod;
for(int i = 1; i <= n; i++)
dist[i] = inf;
dist[start] = 0;
pq.push({0,start});
while(!pq.empty())
{
pair <int, int> _top = pq.top();
pq.pop();
nod = _top.second;
if(viz[nod])
continue;
viz[nod] = 1;
for(auto it:G[nod])
{
if(dist[it.first] > dist[nod] + it.second)
{
dist[it.first] = dist[nod] + it.second;
pq.push({-dist[it.first], it.first});
}
}
}
}
int main()
{
f >> n >> m;
int x, y, z;
for(int i = 1; i <= m; i++)
{
f >> x >> y >> z;
G[x].push_back({y,z});
G[y].push_back({x,z});
}
dijkstra(1);
for(int i = 2; i <= n; i++)
if(dist[i] == inf) g << 0 << " ";
else g << dist[i] << " ";
return 0;
}