Pagini recente » Cod sursa (job #76548) | Cod sursa (job #2340181) | Cod sursa (job #631521) | Cod sursa (job #267164) | Cod sursa (job #2858526)
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair<ll, ll>
#define sz(x) (int)((x).size())
#define all(a) (a).begin(), (a).end()
/* int f[maxn],nf[maxn],inv[maxn];
const int M=998244353;
void init(){
inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
f[0]=nf[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
}
int C(int x,int y){return 1ll*f[x]*nf[y]%M*nf[x-y]%M;}*/
const ll mod = 1e9+7;
ll n, k, m, mi, ma;
const ll N = 2.5e5 + 5;
vector<pi> a[N];
vector<ll> d;
void solve(){
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
cin >> n >> m;
for(int i = 0; i<m; i++){
ll x, y, z;
cin >> x >> y >> z;
x--; y--;
a[x].pb({y, z});
}
set<pi> s;
for(int i = 0; i<n; i++) {
d.pb(mod);
}
d[0] = 0;
s.insert({0, 0});
pi r;
while(!s.empty()){
r = *(s.begin());
s.erase(s.begin());
ll x = r.s;
for(int i = 0; i < a[x].size(); i++){
ll y = a[x][i].f;
if(d[x] + a[x][i].s >= d[y]) continue;
if(s.find({d[y], y}) != s.end()) s.erase( s.find({d[y], y}) );
d[y] = d[x] + a[x][i].s;
s.insert({d[y], y});
}
}
for(int i = 1; i<n; i++) if(d[i] == mod) cout << "0 "; else cout << d[i] << " "; cout << "\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
// cin >> t;
while(t--) solve();
return 0;
}