Pagini recente » Cod sursa (job #2465565) | Cod sursa (job #2652977) | Borderou de evaluare (job #41595) | Borderou de evaluare (job #2178730) | Cod sursa (job #1750054)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 50001;
const int inf = 1 << 30;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector <pair<int,int> >h;
int d[maxn],poz[maxn];
int n,m;
vector <int>a[maxn],cost[maxn];
void citire()
{
f>>n>>m;
int x, y, z;
for ( int i = 1; i <= m; ++i )
{
f>>x>>y>>z;
a[x].push_back(y);
cost[x].push_back(z);
}
}
void dijkstra_heap(int x)
{
for ( int i = 1; i <= n; ++i )
d[i] = inf;
h.push_back(make_pair(0, 1));
int nod,next;
long long cdist;
while (!h.empty())
{
nod = h[0].second;
cdist=-h[0].first;
pop_heap(h.begin(), h.end());
h.pop_back();
for(int i=0; i<a[nod].size(); ++i)
{
next=a[nod][i];
if(d[next]>cdist+cost[nod][i])
{
d[next]=cdist+cost[nod][i];
h.push_back(make_pair(-d[next], next));
push_heap(h.begin(), h.end());
}
}
}
}
int main()
{
citire();
dijkstra_heap(1);
for ( int i = 2; i <= n; ++i )
if (d[i]==inf) g<<0<<" ";
else g<<d[i]<<" ";
}