Pagini recente » Cod sursa (job #2853229) | Cod sursa (job #1914236) | Cod sursa (job #1943256) | Cod sursa (job #323019) | Cod sursa (job #1433834)
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
#define Nmax 50005
#define DIM 666013
char buffer[DIM];
int poz = DIM - 1;
void Scanf(int &A){
A = 0;
while('0' > buffer[poz] || buffer[poz] >'9')
if(++poz == DIM) fread(buffer,1,DIM,stdin),poz = 0;
while('0' <= buffer[poz] && buffer[poz] <= '9'){
A = A * 10 + buffer[poz] - 48;
if(++poz == DIM) fread(buffer,1,DIM,stdin),poz = 0;
}
}
using namespace std;
priority_queue <pair<int,int> > Q;
vector<pair<int,int> >G[Nmax];
vector<int> DP;
int N,M;
void Read()
{
Scanf(N);Scanf(M);
DP.resize(N+1);
int a,b,c;
for(int i = 1; i <= M; ++i){
Scanf(a);Scanf(b);Scanf(c);
G[a].push_back(make_pair(b,c));
}
}
void Dijkstra(int start)
{
int cost = 0;
fill(DP.begin(),DP.end(),INF);
DP[start] = 0;
Q.push(make_pair(0,start));
while(!Q.empty())
{
start = Q.top().second;
cost = -Q.top().first;
Q.pop();
if(DP[start] != cost) continue;
for(auto it : G[start])
if(DP[it.first] > DP[start] + it.second)
{
DP[it.first] = DP[start] + it.second;
Q.push(make_pair(-DP[it.first],it.first));
}
}
for(int i = 2; i <= N; ++i)
if(DP[i] == INF)
printf("0 ");
else
printf("%d ",DP[i]);
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
Read();
Dijkstra(1);
return 0;
}