Pagini recente » Cod sursa (job #1038568) | Cod sursa (job #2391633) | Cod sursa (job #2495216) | Cod sursa (job #522879) | Cod sursa (job #2674678)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
struct ura{
int x, c;
bool operator <(const ura &other) const{
return c > other.c;
}
};
int n;
vector <ura> lista[50005];
priority_queue <ura> pq;
int dist[50005];
int viz[50005];
void dijkstra(int s)
{
int i;
for (i = 1; i <= n; i++)
viz[i] = 0, dist[i] = 2e9;
// viz[s] = 1;
dist[s] = 0;
pq.push({s, 0});
while (!pq.empty())
{
int x = pq.top().x;
pq.pop();
if (viz[x])
continue;
viz[x] = 1;
for (i = 0; i < lista[x].size(); i++)
{
int y = lista[x][i].x;
int c = lista[x][i].c;
if (dist[y] > dist[x] + c)
{
dist[y] = dist[x] + c;
pq.push({y, dist[y]});
}
}
}
for (i = 2; i <= n; i++)
if (dist[i] != 2e9)
cout << dist[i] << " ";
else
cout << 0 << " ";
}
int main()
{
int m, i;
cin >> n >> m;
for (i = 1; i <= m; i++)
{
int x, y, c;
cin >> x >> y >> c;
lista[x].push_back({y, c});
}
dijkstra(1);
return 0;
}