Pagini recente » Cod sursa (job #2074005) | Cod sursa (job #3175843) | Cod sursa (job #2129543) | Cod sursa (job #1982543) | Cod sursa (job #1605024)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector<int> heap;
vector< pair< int , int > > G[10001];
int d[10001] , n , m , x , y , c;
bool cmp(const int& a , const int& b)
{
return d[a] > d[b];
}
int main()
{
f >> n >> m;
for(int i = 1; i <= m; ++i)
{
f >> x >> y >> c;
G[x].push_back(make_pair(y,c));
}
for(int i = 2; i <= n; ++i) d[i] = 1 << 20;
heap.push_back(1);
//make_heap(heap.begin() , heap.end() , cmp);
while(heap.size())
{
int nod = heap.front();
pop_heap(heap.begin() , heap.end() , cmp);
heap.pop_back();
for(auto it = G[nod].begin(); it != G[nod].end(); ++it)
if(d[it -> first] > d[nod] + it -> second)
{
d[it -> first] = d[nod] + it -> second;
heap.push_back(it -> first);
push_heap(heap.begin() , heap.end() , cmp);
}
}
for(int i = 2; i <= n; ++i)
{
if(d[i] == 1 << 20) d[i] = 0;
g << d[i] << " ";
}
return 0;
}