Pagini recente » Cod sursa (job #1494984) | Cod sursa (job #421113) | Cod sursa (job #1381658) | Cod sursa (job #919216) | Cod sursa (job #432455)
Cod sursa(job #432455)
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
#define cost first
#define nod second
#define inf 0x3f3f3f3f
priority_queue< pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > heap;
int n,m;
int cost_min[50002];
vector< pair<int,int> > v[50002];
void dijkstra(int sursa)
{
int nr = 0;
memset(cost_min,inf,sizeof(cost_min));
heap.push( make_pair(0,sursa) );
pair<int,int> nod_curent;
while(!heap.empty())
{
nod_curent = heap.top();
heap.pop();
if(cost_min[nod_curent.nod] != inf)
continue;
cost_min[nod_curent.nod] = nod_curent.cost;
if(++nr == n)
return;
for(vector< pair<int,int> >::iterator it = v[nod_curent.nod].begin(); it != v[nod_curent.nod].end(); ++ it)
heap.push(make_pair(it->cost + nod_curent.cost, (*it).nod));
}
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
scanf("%d %d\n", &n, &m);
int i, x, y, c;
for(i = 1; i <= m; i++)
{
scanf("%d %d %d\n", &x, &y, &c);
v[x].push_back(make_pair(c, y));
//v[y].push_back(make_pair(c, x));
}
dijkstra(1);
for(i = 2; i <= n; i++)
printf("%d ",cost_min[i] != inf ? cost_min[i] : 0);
return 0;
}