Pagini recente » Cod sursa (job #144072) | Cod sursa (job #780244) | Cod sursa (job #970455) | Cod sursa (job #700009) | Cod sursa (job #1042264)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#define N 50005
#define inf (1 << 30)
#define cc first
#define yy second
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
typedef pair <int, int> nod;
vector <nod> a[N];
vector <int> d(N, inf);
vector <bool> viz(N);
priority_queue <nod, vector<nod>, greater<nod> > h;
int main()
{
fin>>n>>m;
while(m--)
{
int x, y, c;
fin>>x>>y>>c;
a[x].push_back(nod(c, y));
}
d[1] = 0; h.push(nod(0, 1));
while(!h.empty())
{
int x = h.top().yy; h.pop();
viz[x] = 1;
for(unsigned i=0; i<a[x].size(); i++)
{
int y = a[x][i].yy;
int cost = a[x][i].cc;
if(!viz[y] && d[y] > d[x] + cost)
{
d[y] = d[x] + cost;
h.push(nod(d[y], y));
}
}
}
for(int i=2; i<=n; i++)
if(d[i] == inf) fout<<0<<' ';
else fout<<d[i]<<' ';
return 0;
}