Pagini recente » Cod sursa (job #3294427) | Cod sursa (job #2682149) | Cod sursa (job #618155) | Cod sursa (job #3289526) | Cod sursa (job #1874924)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int NMAX = 50000 + 5;
const int INFINIT = 1000000000 + 15;
vector < pair < int, int > > C[NMAX];
struct Elem
{
int nod;
int dist;
Elem (int _nod = 0 , int _dist = 0) {nod = _nod; dist=_dist;}
bool operator<(const Elem &arg) const
{
return dist > arg.dist;
}
};
priority_queue<Elem> q;
int viz[NMAX], d[NMAX];
int n , m;
void dijkstra(int x0)
{
int i, j, mini, k, ok;
for (i = 1; i <= n; i++)
{
d[i] = INFINIT;
}
d[x0] = 0;
q.push(Elem(x0, 0));
int node,ceeste, ceafost;
while (!q.empty())
{
node = q.top().nod;
q.pop();
for (int i = 0; i < C[node].size(); ++i)
{
ceeste=d[node]+C[node][i].second;
ceafost=d[C[node][i].first];
if(ceeste < ceafost)
{
d[C[node][i].first] = d[node]+C[node][i].second;
q.push(Elem(C[node][i].first , d[C[node][i].first]));
}
}
}
}
int main()
{
cin >> n >> m;
int a, b,c;
for (int i = 1; i <= m; ++i)
{
cin >> a >> b >> c;
C[a].push_back(make_pair(b, c));
}
dijkstra(1);
for (int i = 2; i <= n; ++i) cout<<d[i]<<" ";
return 0;
}