Pagini recente » Cod sursa (job #333890) | Cod sursa (job #1245964) | Cod sursa (job #1640265) | Cod sursa (job #2017593) | Cod sursa (job #1778081)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define MAX 50010
#define inf 1e9
#define cout fout
typedef vector <pair<int, int> > :: iterator iter;
vector <pair<int, int> > G[MAX];
int dist[MAX];
int nod, i, x, y, c, n, m;
priority_queue<pair<int, int> > Q;
int main()
{
fin >> n >> m;
while(m--)
{
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
for(i = 2 ; i <= n ; i++)
{
dist[i] = inf;
}
dist[1] = 0;
Q.push(make_pair(0, 1));
while(Q.size())
{
pair<int, int> aux = Q.top();
Q.pop();
nod = aux.second;
// cout << nod << " ";
for(iter it = G[nod].begin() ; it != G[nod].end() ; it++)
{
if(dist[it->first] > dist[nod] + it->second)
{
dist[it->first] = dist[nod] + it->second;
Q.push(make_pair(-dist[it->first], it->first));
}
}
}
for(i = 2 ; i <= n ; i++)
{
if(dist[i] == inf)
dist[i] = 0;
cout << dist[i] << ' ';
}
cout << "\n";
}