Pagini recente » Cod sursa (job #1940628) | Cod sursa (job #1000092) | Cod sursa (job #2309020) | Cod sursa (job #13561) | Cod sursa (job #2928018)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int maxn = 50005;
vector <pair <int, int> > v[maxn];
int dist[maxn];
queue <int> q;
/// doar un test, nu e dijkstra
int main()
{
int n, m;
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
in >> x >> y >> c;
v[x].push_back(make_pair(y, c));
v[y].push_back(make_pair(x, c));
}
for(int i = 2; i <= n; i++)
dist[i] = (1 << 30);
q.push(1);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto it : v[nod])
{
if(dist[it.first] > dist[nod] + it.second)
{
dist[it.first] = dist[nod] + it.second;
q.push(it.first);
}
}
}
for(int i = 2; i <= n; i++)
{
if(dist[i] == (1 << 30))
out << 0 << " ";
else
out << dist[i] << " ";
}
return 0;
}