Pagini recente » Cod sursa (job #2389388) | Cod sursa (job #1017651) | Cod sursa (job #1849036) | Cod sursa (job #519981) | Cod sursa (job #968110)
Cod sursa(job #968110)
#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));
a[x].push_back(nod(c, x));
}
d[1] = 0; h.push(nod(0, 1));
while(!h.empty())
{
nod x = h.top(); h.pop();
for(unsigned i=0; i<a[x.yy].size(); i++)
{
int y = a[x.yy][i].yy;
int cost = a[x.yy][i].cc;
if(d[y] > d[x.yy] + cost)
{
d[y] = d[x.yy] + cost;
h.push(nod(d[y], y));
}
}
}
for(int i=2; i<=n; i++)
fout<<d[i]<<' ';
return 0;
}