Pagini recente » Cod sursa (job #1979627) | Cod sursa (job #475513) | Cod sursa (job #2475470) | Cod sursa (job #623779) | Cod sursa (job #1426563)
#include <iostream>
#include <deque>
#include <vector>
#include <cstring>
#include <bitset>
#include <algorithm>
#include <queue>
#define INF 1<<30
#define uint unsigned int
#define ll long long
using namespace std;
int N, M, i, x, y, v, dist[50010];
vector<pair<int,int> > V[50010];
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > Q;
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d%d", &N, &M);
while(M--)
{
scanf("%d%d%d", &x, &y, &v);
V[x].push_back(make_pair(y, v));
}
for(i=1;i<=N;i++)
dist[i] = INF;
dist[1] = 0;
Q.push(make_pair(0, 1));
while(!Q.empty())
{
pair<int,int> now = Q.top();
Q.pop();
if(now.first > dist[now.second])
continue;
for(vector<pair<int,int> >::iterator it = V[now.second].begin();it!=V[now.second].end();it++)
{
if(dist[it->first] > dist[now.second] + it->second)
{
dist[it->first] = dist[now.second] + it->second;
Q.push(make_pair(dist[it->first], it->first));
}
}
}
for(i=2;i<=N;i++)
{
if(dist[i] >= INF)
printf("0 ");
else
printf("%d ", dist[i]);
}
return 0;
}