Pagini recente » Cod sursa (job #2744637) | Cod sursa (job #2579820) | Cod sursa (job #3224694) | Cod sursa (job #1887587) | Cod sursa (job #2871687)
// dijkstra.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
#define ll long long
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
struct ut
{
ll hova, hossz;
};
priority_queue<ut>que;
ut akt;
bool operator<(const ut& a, const ut& b)
{
return a.hossz > b.hossz;
}
struct adat
{
bool lat = false;
ll hossz = LLONG_MAX;
vector < pair<ll, ll>>sz;
};
vector<adat>x;
ll n, m, a, b, c,i;
int main()
{
cin >> n >> m;
x.resize(n + 1);
for (int i = 1; i <= m; ++i)
{
cin >> a >> b>>c;
x[a].sz.push_back({ b,c });
}
que.push({1,0});
while (!que.empty())
{
akt = que.top();
while (x[akt.hova].lat && !que.empty())
{
que.pop();
if (!que.empty())akt = que.top();
}
if (que.empty())break;
que.pop();
x[akt.hova].lat = true;
x[akt.hova].hossz = akt.hossz;
for (auto& e : x[akt.hova].sz)
{
if (!x[e.first].lat && x[e.first].hossz > (akt.hossz + e.second))
{
x[e.first].hossz=akt.hossz + e.second;
que.push({ e.first , akt.hossz + e.second });
}
}
}
for(i=2;i<=n;++i)
cout << x[i].hossz << " ";
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file