Pagini recente » Cod sursa (job #2378399) | Cod sursa (job #354393) | Cod sursa (job #2190313) | Cod sursa (job #920839) | Cod sursa (job #1116161)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#define newn G[nod][i]
#define newc C[nod][i]
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 5e4 + 100;
const int oo = 0x3f3f3f3f;
int N, M;
bool viz[NMAX];
vector <int> G[NMAX], C[NMAX];
vector <int> DIST(NMAX, oo);
typedef pair <int, int> node;
priority_queue <node, vector<node>, greater<node> > Q;
void Dijkstra()
{
DIST[1] = 0; Q.push(node(0, 1));
while(!Q.empty())
{
int nod = Q.top().second; Q.pop();
viz[nod] = 1;
for(unsigned i = 0; i < G[nod].size(); i++)
if(!viz[newn] && DIST[newn] > DIST[nod] + newc)
{
DIST[newn] = DIST[nod] + newc;
Q.push(node(DIST[newn], newn));
}
}
}
int main()
{
fin >> N >> M;
while(M--)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(y);
C[x].push_back(c);
}
Dijkstra();
for(int i = 2; i <= N; i++)
if(DIST[i] != oo)
fout << DIST[i] << ' ';
else fout << "0 ";
return 0;
}