Pagini recente » Cod sursa (job #1744067) | Cod sursa (job #1041248) | Cod sursa (job #3247532) | Cod sursa (job #1885264) | Cod sursa (job #1414302)
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fs first
#define sc second
#define pob pop_back
#define pub push_back
#define eps 1E-7
#define sz(a) a.size()
#define count_one __builtin_popcount;
#define count_onell __builtin_popcountll;
#define fastIO ios_base::sync_with_stdio(false)
#define PI (acos(-1.0))
#define linf (1LL<<62)//>4e18
#define inf (0x7f7f7f7f)//>2e9
#define MAXN 50010
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
vector<pair<int, int> > vec[MAXN];
int dist[MAXN];
void dijkstra() {
for(int i = 2; i <= n; ++i)
dist[i] = inf;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Q;
Q.push(mp(0, 1));
int nod, cost;
while(!Q.empty()) {
nod = Q.top().sc; cost = Q.top().fs; Q.pop();
for(auto it: vec[nod]) {
if(dist[it.sc] > dist[nod] + it.fs) {
dist[it.sc] = dist[nod] + it.fs;
Q.push(mp(dist[it.sc], it.sc));
}
}
}
}
void read() {
fin >> n >> m;
int x, y, z;
while(m--) {
fin >> x >> y >> z;
vec[x].pub(mp(z, y));
}
}
int main() {
read();
dijkstra();
for(int i = 2; i <= n; ++i)
fout << ((dist[i] == inf) ? 0 : dist[i]) << " ";
return 0;
}