Pagini recente » Cod sursa (job #2966128) | Cod sursa (job #2626221) | Cod sursa (job #2781610) | Cod sursa (job #2808221) | Cod sursa (job #807257)
Cod sursa(job #807257)
#include <cstdio>
#include <vector>
#include <queue>
#define NMAX 50001
#define INF 1<<30
using namespace std;
struct nod
{
int v;
int cost;
};
int n;
int cost[NMAX];
struct comp
{
bool operator () (int a, int b)
{
return cost[a] > cost[b];
}
};
vector <nod> G[NMAX];
priority_queue <int, vector <int>, comp> Q;
void read()
{
int m,x;
nod y;
scanf ("%d %d\n", &n, &m);
while (m --)
{
scanf ("%d %d %d\n", &x, &y.v, &y.cost);
G[x].push_back(y);
}
}
void dijkstra()
{
for (int i = 2; i <= n; i++)
cost[i] = INF;
Q.push(1);
while (!Q.empty())
{
int v = Q.top();
Q.pop();
for (int i = 0; i < G[v].size(); i++)
{
nod aux = G[v][i];
if (cost[v] + aux.cost < cost[aux.v])
{
cost[aux.v] = cost[v] + aux.cost;
Q.push(aux.v);
}
}
}
}
void write()
{
for (int i = 2; i <= n; i++)
if (cost[i] == INF)
printf ("0 ");
else
printf ("%d ", cost[i]);
printf ("\n");
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
read();
dijkstra();
write();
return 0;
}