Pagini recente » Cod sursa (job #3314020) | Cod sursa (job #908527) | Cod sursa (job #1893748) | Cod sursa (job #2223989) | Cod sursa (job #3310994)
#include <fstream>
#include <queue>
#include <vector>
#define nmax (int)(1e5+1)
#define inf 1e9
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int n,m,x,y,c,dp[nmax];
bool viz[nmax];
struct muchie{
int x,c;
};
vector<muchie>v[nmax];
struct cmp{
bool operator()(const muchie& a,const muchie& b){
return a.c>b.c;
}
};
priority_queue<muchie,vector<muchie>,cmp>q;
void dijkstra(){
for(int i=2;i<=n;i++)
dp[i]=inf;
q.push({1,0});
while(!q.empty()){
int nod=q.top().x;
q.pop();
if(viz[nod])
continue;
viz[nod]=1;
for(auto i:v[nod])
if(!viz[i.x]&&dp[i.x]>dp[nod]+i.c){
dp[i.x]=dp[nod]+i.c;
q.push({i.x,dp[i.x]});
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>x>>y>>c;
v[x].push_back({y,c});
}
dijkstra();
for(int i=2;i<=n;i++){
if(dp[i]==inf)
dp[i]=0;
cout<<dp[i]<<" ";
}
return 0;
}