Pagini recente » Cod sursa (job #2339909) | Cod sursa (job #83183) | Cod sursa (job #2647847) | Cod sursa (job #374278) | Cod sursa (job #3159069)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nmax = 250000;
struct elem
{
int nod, val;
bool operator < (const elem &other) const
{
return val > other.val;
}
};
vector<elem> g[nmax + 5];
int distMin[nmax + 5];
int n, m;
void Dijkstra()
{
priority_queue<elem> coada;
coada.push({1, 0});
while(!coada.empty())
{
int nod = coada.top().nod;
int val = coada.top().val;
coada.pop();
if(val != distMin[nod])
continue;
for(auto it : g[nod])
{
if(val + it.val < distMin[it.nod])
{
distMin[it.nod] = val + it.val;
coada.push({it.nod, val + it.val});
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= n; i ++)
{
distMin[i] = INT_MAX;
}
distMin[1] = 0;
while(m--)
{
int a, b, x;
fin >> a >> b >> x;
g[a].push_back({b, x});
}
Dijkstra();
for(int i = 2; i <= n; i ++)
{
if(distMin[i] == INT_MAX)
distMin[i] = 0;
fout << distMin[i] << ' ';
}
}