Pagini recente » Cod sursa (job #2131832) | Cod sursa (job #247384) | Cod sursa (job #1565837) | Cod sursa (job #2450398) | Cod sursa (job #2203182)
#define DM 500001
#define inf 0x3f3f3f3f
#include <cstring>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fi ("dijkstra.in");
ofstream fo ("dijkstra.out");
int n, m, dst[DM], a, b, c;
struct str
{
int n, c;
bool operator < (const str &other) const
{
return c > other.c;
}
} x;
priority_queue <str> pq;
vector <str> v[DM];
void dijkstra()
{
dst[1] = 0;
pq.push({1, 0});
while (!pq.empty())
{
x = pq.top();
pq.pop();
if (dst[x.n] < x.c)
continue;
for (auto i:v[x.n])
if (i.c + x.c < dst[i.n])
{
dst[i.n] = x.c + i.c;
pq.push({i.n, dst[i.n]});
}
}
}
int main()
{
fi >> n >> m;
for (int i = 1; i <= n; ++i)
{
fi >> a >> b >> c;
v[a].push_back({b,c});
v[b].push_back({a, c});
}
memset(dst, inf, sizeof(dst));
dijkstra();
for (int i = 2; i <= n; ++i)
fo << dst[i] << ' ';
return 0;
}