Pagini recente » Cod sursa (job #1412640) | Cod sursa (job #1684241) | Cod sursa (job #366004) | Cod sursa (job #2968950) | Cod sursa (job #2505879)
#define inf 0x3f3f3f3f
#define NMAX 50005
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int n, m;
int Dmin[NMAX], viz[NMAX];
struct elem{
int y, c;
bool operator<(const elem &other) const {
if(c != other.c)
return c > other.c;
return y > other.y;
}
};
vector<pair<int, int>> graph[NMAX];
priority_queue <elem, vector<elem>> Q;
void read()
{
int x, y, c;
scanf("%d %d", &n, &m);
for(int i=0; i<m; ++i){
scanf("%d %d %d", &x, &y, &c);
graph[x].push_back({y, c});
}
}
void solve()
{
Q.push({1, 0});
viz[1] = 1;
while(!Q.empty())
{
elem t = Q.top();
Q.pop();
viz[t.y] = 0;
for(auto &v:graph[t.y])
if(Dmin[v.first] > Dmin[t.y] + v.second)
{
Dmin[v.first] = Dmin[t.y] + v.second;
if(viz[v.first] == 0)
{
Q.push({v.first, Dmin[v.first]});
viz[v.first] = 1;
}
}
}
}
void init()
{
for(int i=2; i<=n; ++i)
Dmin[i] = inf;
}
void write()
{
for(int i=2; i<=n; ++i)
{
if(Dmin[i] == inf)
printf("0 ");
else printf("%d ", Dmin[i]);
}
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
read();
init();
solve();
write();
return 0;
}