Pagini recente » Cod sursa (job #633449) | Cod sursa (job #458690) | Cod sursa (job #248901) | Cod sursa (job #270981) | Cod sursa (job #1110778)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#define inf 2000000000
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int N, M, D[50002], x;
struct muchie{ int y, c; }aux;
vector <muchie> G[50002];
struct cmp
{
bool operator()(const int a, const int b)const
{
return (D[a]>D[b]);
}
};
priority_queue < int, vector<int>, cmp>Q;
void dijkstra(int sursa)
{
for (int i=1; i<=N; ++i)
D[i]=inf; D[sursa]=0; Q.push(sursa);
while (Q.size())
{
int nod=Q.top(); Q.pop();
vector<muchie>::iterator it=G[nod].begin();
for (; it!=G[nod].end(); ++it)
if (D[nod]+it->c < D[it->y])
D[it->y]=D[nod]+it->c, Q.push(it->y);
}
}
int main()
{
f>>N>>M;
for (int i=1; i<=M; ++i)
f>>x>>aux.y>>aux.c, G[x].push_back(aux);
dijkstra(1);
for (int i=2; i<=N; ++i)
if (D[i]!=inf) g<<D[i]<<' ';
else g<<"0 ";
return 0;
}