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