Pagini recente » Cod sursa (job #1279483) | Cod sursa (job #267924) | Cod sursa (job #1691479) | Cod sursa (job #2116475) | Cod sursa (job #2970060)
#include <bits/stdc++.h>
using namespace std;
#define nmax 50000
#define inf 1e18
#define ll long long
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n;
vector<vector<pair<ll,int>>> dk(nmax+5);
ll dp[nmax+5];
void dijkstra(){
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>>pq;
dp[1]=0;
pq.push({0,1});
while(pq.size()){
pair<ll,int>now=pq.top();
pq.pop();
if(now.first!=dp[now.second])continue;
for(auto i:dk[now.second]){
pair<ll,int>nxt=i;
if(dp[nxt.second]>now.first+nxt.first){
dp[nxt.second]=now.first+nxt.first;
pq.push({now.first+nxt.first,nxt.second});
}
}
}
}
int main()
{
int m;
f>>n>>m;
for(int i=1;i<=m;++i)
{
int x,y;
ll c;
f>>x>>y>>c;
dk[x].emplace_back(c,y);
}
for(int i=1;i<=n;++i)
dp[i]=inf;
dijkstra();
for(int i=2;i<=n;++i)
if(dp[i]==inf)g<<"0";
else g<<dp[i]<<" ";
}