Pagini recente » Cod sursa (job #864374) | Cod sursa (job #2899615) | Cod sursa (job #1759470) | Cod sursa (job #1518340) | Cod sursa (job #3266158)
#include <fstream>
#include <deque>
#include <vector>
using namespace std;
int cost[50001];
deque <int> Nod;
vector <pair<int,int>> V[500001];
int main()
{
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, a, b, c;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> a >> b >> c;
V[a].push_back(make_pair(b, c));
V[b].push_back(make_pair(a, c));
}
cost[1] = 0;
Nod.push_back(1);
for (int i = 2; i <= n; i++)
cost[i] = 1000000005;
while (!Nod.empty())
{
int currentNode = Nod.front();
Nod.pop_front();
for (auto x : V[currentNode])
{
if (cost[x.first] > cost[currentNode] + x.second)
{
cost[x.first] = cost[currentNode] + x.second;
Nod.push_back(x.first);
}
}
}
for (int i = 2; i <= n; i++)
fout << cost[i] << " ";
return 0;
}