Pagini recente » Cod sursa (job #1543868) | Cod sursa (job #1855330) | Cod sursa (job #2096235) | Profil Rodik_Rody | Cod sursa (job #1364306)
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#define DIM 1666013
#define Nmax 50005
#define INF 0x3f3f3f3f
using namespace std;
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;
}
}
priority_queue<pair<int,int> > Q;
vector<vector<pair<int,int> > > G;
vector<int> DP;
int N,M;
void Read()
{
Scanf(N);Scanf(M);
G.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(c,b));
}
}
void Dijkstra(int k)
{
DP.resize(N+1);
DP.assign(N+1,INF);
DP[1] = 0;
Q.push(make_pair(0,1));
while(!Q.empty())
{
k = Q.top().second; Q.pop();
for(vector<pair<int,int> >::iterator it = G[k].begin(); it != G[k].end(); ++it)
if(DP[it->second] > DP[k] + it->first)
{
DP[it->second] = DP[k] + it->first;
Q.push(make_pair(-DP[it->second],it->second));
}
}
for(int i = 2; i <= N; ++i)
if(DP[i] != INF)
printf("%d ",DP[i]);
else
printf("0 ");
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
Read();
Dijkstra(1);
return 0;
}