Pagini recente » Cod sursa (job #2785369) | Cod sursa (job #933367) | Cod sursa (job #1584320) | Cod sursa (job #1732316) | Cod sursa (job #3193957)
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
using pereche=pair<int, int>;
const int nMax=(int)5e4 + 5, valMax=INT_MAX;
int n, m, x, y, c, d[nMax];
vector<pereche> g[nMax];
void dijkstra(int start)
{
priority_queue<pereche, vector<pereche>, greater<pereche> > pq;
vector<bool> viz=vector<bool>(n+1, 0);
for(int i=1; i<=n; i++)
d[i]=valMax;
d[start]=0;
pq.push({d[start], start});
while(!pq.empty())
{
int cost=pq.top().first;
int nod=pq.top().second;
pq.pop();
if(!viz[nod])
{
viz[nod]=1;
for(auto i: g[nod])
{
int nodNou=i.first;
int costNou=i.second;
if(d[nodNou] > d[nod] + costNou)
{
d[nodNou]=d[nod] + costNou;
pq.push({d[nodNou], nodNou});
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(0);
fin>>n>>m;
for(int i=0; i<m; i++)
{
fin>>x>>y>>c;
g[x].push_back({y, c});
}
fin.close();
dijkstra(1);
for(int i=2; i<=n; i++)
if(d[i] == valMax)
fout<<0<<' ';
else fout<<d[i]<<' ';
fout.close();
return 0;
}