Pagini recente » Cod sursa (job #1331755) | Cod sursa (job #2793698) | Cod sursa (job #2265788) | Cod sursa (job #828586) | Cod sursa (job #2377960)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const long long INF = 5000000001;
const int N = 50001;
const int M = 250001;
long long n, m, x, y, c, d[N], sel[N];
vector <pair<long long, long long> > a[N];
priority_queue<pair<long long, long long>> h;
void dijkstra(int x0)
{
for(int i=1; i<=n; i++)
d[i] = INF;
d[x0] = 0;
h.push(make_pair(-d[x0], x0));
while(!h.empty())
{
while(!h.empty() && sel[h.top().second])
h.pop();
if(h.empty()) return;
x = h.top().second;
sel[x] = true;
///for(auto p: a[x])
for (size_t i = 0; i < a[x].size(); i++)
{
y = a[x][i].first;
c = a[x][i].second;
if(d[x] + c < d[y])
{
d[y] = d[x] + c;
h.push(make_pair(-d[y], y));
}
}
}
}
int main()
{
in>>n>>m;
for(int i=1; i<=m; i++)
{
in>>x>>y>>c;
a[x].push_back(make_pair(y, c));
}
dijkstra(1);
for(int i=2; i<=n; i++)
out<<d[i]<<' ';
return 0;
}