Pagini recente » Cod sursa (job #184571) | Monitorul de evaluare | Cod sursa (job #312958) | Cod sursa (job #3333200) | Cod sursa (job #3335203)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <set>
#include <algorithm>
#define inf 2e9
using namespace std;
ifstream in ("dijkstra.in");
ofstream out ("dijkstra.out");
struct Muchie
{
int x, y, cost;
};
struct Node
{
int x, cost;
};
bool operator<(const Node& a, const Node& b)
{
if (a.cost == b.cost)
return a.x < b.x;
return a.cost < b.cost;
}
bool CompareMuchii(const Muchie& a, const Muchie& b)
{
if (a.cost == b.cost)
{
if (a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
return a.cost < b.cost;
}
vector<int> Dijkstra(int start, int n, vector<vector<Node>> &listaVecini)
{
vector<int> d(n, inf);
d[start] = 0;
set<Node> Set;
Set.emplace(start, 0);
while (!Set.empty())
{
Node nod = *Set.begin();
Set.erase(Set.begin());
for (Node v : listaVecini[nod.x])
if (d[nod.x] + v.cost < d[v.x])
{
if (d[v.x] != inf)
Set.erase(Set.find(Node(v.x, d[v.x])));
d[v.x] = d[nod.x] + v.cost;
Set.emplace(v.x,d[v.x]);
}
}
return d;
}
int main()
{
int n, m; in >> n >> m;
vector<Muchie> listaMuchii(m);
vector<vector<Node>> listaVecini(n);
for (int i = 0; i < m; i++)
{
int x, y, cost; in >> x >> y >> cost;
x--; y--;
listaMuchii[i] = Muchie(x, y, cost);
listaVecini[x].emplace_back(y, cost);
listaVecini[y].emplace_back(x, cost);
}
vector<int> d = Dijkstra(0, n, listaVecini);
for (int i = 1; i < n; i++)
out << ((d[i] != inf) ? d[i] : 0) << " ";
out << "\n";
return 0;
}