Pagini recente » Cod sursa (job #298921) | Cod sursa (job #2815503) | Cod sursa (job #400331) | Cod sursa (job #668372) | Cod sursa (job #2953930)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int INF = 1e9;
struct node
{
int nod, cost;
bool operator < (const node &x) const
{
return cost > x.cost;
}
};
priority_queue < node > Q;
vector < node > a[50001];
int d[50001], viz[50001], n, m, x, y, z;
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> x >> y >> z;
a[x].push_back({y, z});
}
for(int i = 2; i <= n; i++)
{
d[i] = INF;
}
d[1] = 0;
Q.push({1, 0});
while(!Q.empty())
{
node p = Q.top();
Q.pop();
if(viz[p.nod])
continue;
viz[p.nod] = 1;
for(auto it : a[p.nod])
{
if(d[it.nod] > d[p.nod] + it.cost)
{
d[it.nod] = d[p.nod] + it.cost;
Q.push({it.nod, d[it.nod]});
}
}
}
for(int i = 2; i <= n; i++)
{
fout << (d[i] == INF ? 0 : d[i]) << ' ';
}
return 0;
}