Pagini recente » Cod sursa (job #1233888) | Cod sursa (job #2446650) | Cod sursa (job #2221642) | Cod sursa (job #3201098) | Cod sursa (job #2964888)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll NMAX=5e4+5;
set<pll> s;
ll dist[NMAX];
vector<pll> edg[NMAX];
int main()
{
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
ll n,m;
fin>>n>>m;
for(ll i=0;i<m;i++){
ll u,v,c;
fin>>u>>v>>c;
u--,v--;
edg[u].push_back({v,c});
}
for(ll i=1;i<n;i++)
dist[i]=1e12;
s.insert({dist[0],0});
while(!s.empty()){
ll u=s.begin()->second;
s.erase(s.begin());
for(auto it : edg[u]){
if(dist[u]+it.second<dist[it.first]){
s.erase({dist[it.first],it.first});
dist[it.first]=dist[u]+it.second;
s.insert({dist[it.first],it.first});
}
}
}
for(ll i=1;i<n;i++)
if(dist[i]>=1e10) fout<<"0 ";
else fout<<dist[i]<<' ';
return 0;
}