Pagini recente » Cod sursa (job #1870639) | Clasament teme_acmunibuc_2013 | Cod sursa (job #1458461) | Cod sursa (job #499124) | Cod sursa (job #1855706)
#include <bits/stdc++.h>
using namespace std;
int n, m, dp[50005];
const int inf = 2000000000;
struct el
{
int nod, cost;
bool operator < (const el &A) const
{
return cost > A.cost;
}
};
priority_queue <el> q;
vector <int> G[50005], C[50005];
inline void Citire()
{
int i, x, y, z;
ifstream fin("dijkstra.in");
fin >> n >> m;
for(i = 2; i <= n; i++) dp[i] = inf;
for(i = 1; i <= m; i++)
{
fin >> x >> y >> z;
G[x].push_back(y);
C[x].push_back(z);
}
}
inline void BFS()
{
el A, B;
int nod, cost;
unsigned int i;
A.cost = 0, A.nod = 1;
q.push(A);
while(!q.empty())
{
A = q.top();
nod = A.nod;
q.pop();
for(i = 0; i < G[nod].size(); i++)
{
cost = dp[nod] + C[nod][i];
if(dp[G[nod][i]] > cost)
{
B.nod = G[nod][i];
B.cost = cost;
dp[B.nod] = cost;
q.push(B);
}
}
}
}
inline void Afisare()
{
ofstream fout("dijkstra.out");
for(int i = 2; i <= n; i++)
if (dp[i] == inf) fout << "0 ";
else fout << dp[i] << " ";
fout<<"\n";
fout.close();
}
int main()
{
Citire();
BFS();
Afisare();
return 0;
}