Pagini recente » Cod sursa (job #871854) | Cod sursa (job #2235362) | Cod sursa (job #1515019) | Cod sursa (job #296742) | Cod sursa (job #2499807)
#include <fstream>
#include <iostream>
#include <queue>
#define NMAX 50000
#define inf 2000000000
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m, dist[NMAX+10];
bool b[NMAX+10];
priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > pq;
vector <pair <int, int> > nod[NMAX+10];
int main()
{
f >> n >> m;
for(int i=1; i<=m; i++)
{ int nod1, nod2, cost;
f >> nod1 >> nod2 >> cost;
nod[nod1].push_back(make_pair(cost, nod2));
}
pq.push(make_pair(0, 1));
for(int i=2; i<=n; i++) dist[i] = inf;
while(!pq.empty())
{ pair <int, int> a = pq.top();
pq.pop();
if(!b[a.second])
{ b[a.second] = 1;
for(int i=0; i<nod[a.second].size(); i++)
{ int d = a.first + nod[a.second][i].first;
if(d < dist[nod[a.second][i].second])
{ dist[nod[a.second][i].second] = d;
pq.push(make_pair(d, nod[a.second][i].second));
}
}
}
}
for(int i=2; i<=n; i++)
{ if(dist[i] != inf) g << dist[i] << ' ';
else g << 0 << ' ';
}
g << '\n';
return 0;
}