Pagini recente » Cod sursa (job #2900945) | Cod sursa (job #908058) | Cod sursa (job #2801122) | Cod sursa (job #1651320) | Cod sursa (job #2228075)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, d[50005];
vector < pair < int, int > > graph[50005];
bool inQ[50005];
struct comp{
bool operator()(int x, int y)
{
return d[x] > d[y];
}
};
priority_queue <int, vector <int>, comp> Q;
int main()
{
int x, y, c, k;
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> x >> y >> c;
graph[x].push_back(make_pair(y,c));
}
for(int i = 1; i <= n; i++)
d[i] = 1000000005;
d[1] = 0;
Q.push(1);
inQ[1] = true;
while(!Q.empty())
{
int cnd = Q.top();
Q.pop();
inQ[k] = false;
for(auto it : graph[cnd])
{
k = it.first;
c = it.second;
if(d[cnd] + c < d[k])
{
d[k] = d[cnd] + c;
if(inQ[k] == false)
{
Q.push(k);
inQ[k] = true;
}
}
}
}
for(int i = 2; i <= n; i++)
if(d[i] == 1000000005)
fout << "0 ";
else
fout << d[i] << ' ';
return 0;
}