Pagini recente » Cod sursa (job #1117341) | Cod sursa (job #2581493) | Cod sursa (job #3172965) | Cod sursa (job #39959) | Cod sursa (job #2864226)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nmax = 50005;
const int oo = 1e9;
int n, m;
vector<pair<int, int>>V[nmax];
priority_queue<pair<int, int>>pq;
int dist[nmax];
int viz[nmax];
void Dijkstra()
{
int i, j, x, y, k;
for(i = 1;i <= n;i++)
dist[i] = oo;
dist[1] = 0;
pq.push({0, 1});
while(!pq.empty())
{
k = pq.top().second;
pq.pop();
if(viz[k] == 0)
{
viz[k] = 1;
for(auto x : V[k])
if(dist[x.first] > dist[k] + x.second)
{
dist[x.first] = dist[k] + x.second;
pq.push({-dist[x.first], x.first});
}
}
}
for(i = 2;i <= n;i++)
fout << dist[i] << " ";
}
void Citire()
{
int x, y, cost, i;
fin >> n >> m;
for(i = 1;i <= m;i++)
{
fin >> x >> y >> cost;
V[x].push_back({y, cost});
}
}
int main()
{
Citire();
Dijkstra();
return 0;
}