Pagini recente » Cod sursa (job #524523) | Cod sursa (job #437603) | Istoria paginii runda/dedicatie_speciala9/clasament | Cod sursa (job #439490) | Cod sursa (job #2739497)
#include <bits/stdc++.h>
using namespace std;
const int INF(1 << 30), NMAX(50005);
using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
void BUNA(const string& task = "")
{
if (!task.empty())
freopen((task + ".in").c_str(), "r", stdin),
freopen((task + ".out").c_str(), "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void PA()
{
exit(0);
}
VI viz(NMAX, 0), cost(NMAX, INF);
vector<pair<int, int> > G[NMAX];
void Dijkstra()
{
set<pair<int, int > > s;
s.insert({0, 1});
cost[1] = 0;
while(!s.empty())
{
int nod = s.begin()->second;
s.erase(s.begin());
viz[nod] = 1;
for(auto it: G[nod])
if(viz[it.first] == 0)
{
int newC = cost[nod] + it.second;
if(newC < cost[it.first])
{
if(cost[it.first] != INF)
s.erase(s.find({cost[it.first], it.first}));
cost[it.first] = newC;
s.insert({cost[it.first], it.first});
}
}
}
}
int main()
{
BUNA("dijkstra");
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; ++i)
{
int x, y, c;
cin >> x >> y >> c;
G[x].push_back({y, c});
}
Dijkstra();
for(int i = 2; i <= n; ++i)
if(cost[i] == INF)
cout << 0 << ' ';
else cout << cost[i] << ' ';
PA();
}