Pagini recente » Cod sursa (job #2033173) | Cod sursa (job #340565) | Cod sursa (job #550591) | Cod sursa (job #3234156) | Cod sursa (job #2353593)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define inf 0x3f3f3f3f
#define N 250005
using namespace std;
int n, m, cost[N], viz[N];
struct cmp
{
bool operator()(int a, int b)
{
return cost[a]>cost[b];
}
};
vector <pair <int, int> > g[N];
vector <pair <int, int> > ::iterator it;
priority_queue <int, vector<int>, cmp> heap;
void citire()
{
scanf("%d %d\n", &n, &m);
for(int i=0;i<m;i++)
{
cost[i]=inf;
int x, y, c;
scanf("%d %d %d\n", &x, &y, &c);
g[x].push_back({y, c});
}
}
void parcurg(int nod)
{
for(it=g[nod].begin();it!=g[nod].end();it++)
if(cost[it->first]>cost[nod]+it->second)
{
cost[it->first]=cost[nod]+it->second;
if(!viz[it->first])
{
viz[it->first]=1;
heap.push(it->first);
}
}
}
void dijkstra()
{
cost[1]=0;
heap.push(1);
while(!heap.empty())
{
int nod=heap.top();
heap.pop();
viz[nod]=0;
parcurg(nod);
}
}
void afisare()
{
for(int i=2;i<=n;i++)
{
if(cost[i]==inf)
cost[i]=0;
printf("%d ", cost[i]);
}
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
citire();
dijkstra();
afisare();
return 0;
}