Pagini recente » Cod sursa (job #542236) | Cod sursa (job #1565043) | Cod sursa (job #1772646) | Cod sursa (job #1951131) | Cod sursa (job #1823764)
#include <bits/stdc++.h>
using namespace std;
class cmp
{
public:
bool operator ()(const pair <int, long long> &a, const pair<int, long long> &b)
{
return a.second>b.second;
}
};
priority_queue < pair <int, long long>, vector <pair<int, long long>>, cmp> pq;
int n, m;
vector <pair <int, int>> graf[50001];
long long dist[50001];
bool frecv[50001];
void initializare()
{
int i;
for (i = 2; i<=n; ++i)
dist[i] = 99999999999;
}
int main()
{
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
fin >> n >> m;
initializare();
int y, i, j, x, nod, cost, d;
for (i = 0; i<m; ++i)
{
fin >> x >> y >> d;
graf[x].push_back (make_pair(y, d));
graf[y].push_back (make_pair(x, d));
}
pq.push(make_pair(1, dist[1]));
while (pq.size())
{
nod = pq.top().first;
frecv[nod] = 1;
cost = pq.top().second;
for (auto x : graf[nod])
{
if (frecv[x.first] == 0){
if (cost + x.second < dist[x.first])
dist[x.first] = cost+x.second;
pq.push(make_pair(x.first, dist[x.first]));}
}
pq.pop();
}
for (i = 2; i<=n; ++i)
{if (dist[i] != 99999999999)
fout << dist[i] << " ";
else fout << "0 ";}
return 0;
}