Pagini recente » Cod sursa (job #467419) | Cod sursa (job #2124298) | Cod sursa (job #2724911) | Cod sursa (job #1413558) | Cod sursa (job #2737737)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int maxn = 50005;
vector <pair <int, int> > v[maxn];
priority_queue <pair <int, int> > pq;
int drum[maxn];
int main()
{
int n, m;
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, cost;
in >> x >> y >> cost;
v[x].push_back(make_pair(y, cost));
}
for(int i = 2; i <= n; i++)
drum[i] = (1 << 30);
pq.push(make_pair(0, 1));
while(!pq.empty())
{
pair <int, int> p = pq.top();
int cst = -p.first;
int nod = p.second;
pq.pop();
if(drum[nod] < cst)
continue;
for(auto it : v[nod])
{
if(drum[it.first] > cst + it.second)
{
drum[it.first] = cst + it.second;
pq.push(make_pair(-drum[it.first], it.first));
}
}
}
for(int i = 2; i <= n; i++)
{
if(drum[i] == (1 << 30))
out << "0 ";
else
out << drum[i] << " ";
}
out << "\n";
return 0;
}