Pagini recente » Cod sursa (job #2956746) | Cod sursa (job #1972692) | Cod sursa (job #2953653) | Cod sursa (job #990445) | Cod sursa (job #2968124)
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <bitset>
#include <map>
#include <cstring>
#include <algorithm>
#define NMAX 50003
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
int dMax[NMAX];
bool viz[NMAX];
struct muchie {
int nod;
int cost;
bool operator() (const muchie a, const muchie b) const
{
return a.cost > b.cost;
}
};
vector<muchie>graf[NMAX];
priority_queue<muchie, vector<muchie>, muchie> coada;
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, cost;
fin >> x >> y >> cost;
muchie a= { y,cost };
graf[x].push_back(a);
}
for (int i = 1; i <= n; i++)
{
dMax[i] = INT_MAX;
}
coada.push({ 1,0 });
dMax[1] = 0;
while (!coada.empty())
{
int prec = coada.top().nod;
int cPrec = coada.top().cost;
coada.pop();
if (viz[prec] == true)
{
continue;
}
viz[prec] = true;
for (auto itr = graf[prec].begin(); itr != graf[prec].end(); itr++)
{
int nd = itr->nod;
int cMuchie = itr->cost;
if (!viz[nd] && dMax[nd] > cPrec + cMuchie)
{
dMax[nd] = cPrec + cMuchie;
coada.push({ nd,dMax[nd] });
}
}
}
for (int i = 2; i <= n; i++)
{
if (dMax[i] == INT_MAX)
{
dMax[i] = 0;
}
fout << dMax[i]<<" ";
}
return 0;
}