Pagini recente » Cod sursa (job #680104) | Cod sursa (job #2518790) | Cod sursa (job #673275) | Cod sursa (job #2250999) | Cod sursa (job #3166488)
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
#define fi first
#define se second
#define pb push_back
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int N = 5e4+3;
const int oo = 0x3f3f3f3f;
int n;
vector<vector<pii>> e;
vector<int> cnt, cst;
void read(), dijkstra();
int main()
{
read();
dijkstra();
for (int i = 2; i <= n; i++) g << cst[i] << ' ';
return 0;
}
void read(){
int m;
f >> n >> m;
e.resize(n+3); cnt.resize(n+3); cst.resize(n+3, oo);
while (m--){
int a, b, c; f >> a >> b >> c;
e[a].pb({b,c});
}
}
void dijkstra(){
set<pii> s; s.insert({0, 1});
while (!s.empty()){
int from, c;
tie(c,from) = *s.begin(); s.erase(s.begin());
for (auto it: e[from]){
int to, cm; tie(to, cm) = it;
if (cm + c < cst[to]){
if (cst[to] != oo) s.erase({cst[to], to});
cst[to] = c + cm;
s.insert({cst[to], to});
}
}
}
}