Pagini recente » Cod sursa (job #1154353) | Cod sursa (job #71121) | Cod sursa (job #240128) | Istoria paginii runda/allyoucancode2008 | Cod sursa (job #2207414)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
const int N = 50005;
const int inf = 1 << 30;
struct lat
{
int y, l;
};
vector<lat> g[N];
int n, m;
int viz[N], d[N];
priority_queue<pair<int, int>> pq;
int main()
{
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
///citire
fin>>n>>m;
while(m--)
{
int x,y,l;
fin>>x>>y>>l;
g[x].push_back({y,l});
}
///initializare
for(int i=2;i<=n;i++)
d[i] = inf;
///dijkstra
pq.push({0, 1});
while(!pq.empty())
{
int x = pq.top().second;
pq.pop();
viz[x]++;
if(viz[x] > 1)
continue;
for(auto y : g[x])
if(!viz[y.y] && (d[y.y] == inf || d[y.y] > d[x] + y.l))
{
d[y.y] = d[x] + y.l;
pq.push({-d[y.y], y.y});
}
}
///afisare
for(int i=2;i<=n;i++)
fout<<d[i]<<' ';
return 0;
}