Pagini recente » Cod sursa (job #1912424) | Cod sursa (job #1254850) | Cod sursa (job #534673) | Cod sursa (job #2241956) | Cod sursa (job #3030064)
#include <bits/stdc++.h>
#include <fstream>
#define pii pair<int,int>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int N=1e5+5;
int n,m,d[N];
bool inq[N];
vector<pii> v[N];
void djk(int x0)
{
for(int i=1;i<=n;i++)
d[i]=2e9;
d[x0]=0;
priority_queue<pii> q;
q.push({0,x0});
while(!q.empty())
{
int x=q.top().second;
q.pop();
inq[x]=true;
for(auto it:v[x])
{
int y=it.first;
int c=it.second;
if(d[x]+c<d[y])
{
d[y]=d[x]+c;
if(!inq[y])
{
q.push({d[y],y});
inq[y]=true;
}
}
}
}
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,c;
fin>>x>>y>>c;
v[x].push_back({y,c});
}
djk(1);
for(int i=2;i<=n;i++)
{
if(d[i]==2e9)
fout<<"0 ";
else
fout<<d[i]<<" ";
}
return 0;
}