Pagini recente » Cod sursa (job #1138133) | Cod sursa (job #2341889) | Cod sursa (job #1176354) | Cod sursa (job #2027045) | Cod sursa (job #2280224)
#include <bits/stdc++.h>
using namespace std;
ifstream f("date.in");
const int nmax = 100000;
const int inf = 1000000;
vector < pair < int, int > > G[nmax];
int D[nmax],n,m;
void dijkstra()
{
priority_queue < pair < int, int>, vector < pair < int, int > >, greater < pair <int, int> > > pq;
pq.emplace(0, 1);
while(!pq.empty())
{
int nod= pq.top().second;
int val= pq.top().first;
pq.pop();
if(D[nod] == val)
{
for( auto &it: G[nod])
{
if(val + it.second < D[it.first])
{
D[it.first] = val + it.second;
pq.emplace(D[it.first], it.first);
}
}
}
}
}
int main()
{
int x,y,c;
f>>n>>m;
for(int i=2; i<nmax; i++)
D[i]=inf;
for(int i=1; i<=m; i++)
{
f>>x>>y>>c;
G[x].push_back(make_pair(y,c));
}
dijkstra();
for(int i=2; i<=n; i++)
{
if(D[i]==inf)
cout<<0<<' ';
else
cout<<D[i]<<' ';
}
return 0;
}