Pagini recente » Cod sursa (job #962099) | Cod sursa (job #1753702) | Cod sursa (job #637048) | Cod sursa (job #1302069) | Cod sursa (job #1915572)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define DM 50005
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int INF = (1 << 30);
int n, m, a, b, c, dist[DM];
vector<pair<int, int> > g[DM];
priority_queue<pair<int, int> > pq;
bool viz[DM];
void dijkstra()
{
for(int i = 2; i <= n; i++)
dist[i] = INF;
pq.push({0,1});
while(!pq.empty())
{
int nod = pq.top().second;
pq.pop();
if(!viz[nod])
{
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()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> a >> b >> c;
g[a].push_back({b,c});
g[b].push_back({a,c});
}
dijkstra();
for(int i = 2; i <= n; i++)
fout << dist[i] <<' ';
return 0;
}