Pagini recente » Cod sursa (job #3249178) | Cod sursa (job #1703138) | Cod sursa (job #208682) | Cod sursa (job #457706) | Cod sursa (job #3284934)
#include <fstream>
#include <vector>
#include <queue>
#define inf 2e9
#define nmax 100001
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int n,m,x,y,c,dp[nmax];
vector<pair<int,int>>v[nmax];
bool viz[nmax];
struct cmp{
bool operator()(const pair<int,int>& a,const pair<int,int>& b){
return a.second>b.second;
}
};
priority_queue<pair<int,int>,vector<pair<int,int>>,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().first;
q.pop();
if(viz[nod])
continue;
viz[nod]=1;
for(auto i:v[nod])
if(!viz[i.first]&&dp[i.first]>dp[nod]+i.second){
dp[i.first]=dp[nod]+i.second;
q.push({i.first,dp[i.first]});
}
}
}
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)
cout<<"0 ";
else
cout<<dp[i]<<" ";
return 0;
}