Pagini recente » Cod sursa (job #2194057) | Cod sursa (job #1386853) | Cod sursa (job #1660359) | Cod sursa (job #2779919) | Cod sursa (job #2230639)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
#define Gmax 50010
#define INF 0x3f3f3f3f
struct point{
int nod;
int cost;
};
bool operator<(point a,point b)
{
return a.cost > b.cost;
}
vector <pair <int,int> > G[Gmax];
priority_queue <point> pq;
int dist[Gmax];
int main()
{
int N,M;
f>>N>>M;
for(int i=1;i<=M;i++)
{
int x,y,z;
f>>x>>y>>z;
G[x].push_back(make_pair(y,z));
}
memset(dist,0x3f,sizeof(dist));
pq.push({1,0});
while(!pq.empty())
{
int nod = pq.top().nod;
int d = pq.top().cost;
pq.pop();
if(dist[nod] != INF)
continue;
dist[nod] = d;
for(int i=0; i < G[nod].size(); i++)
{
pq.push({G[nod][i].first,d + G[nod][i].second});
}
}
for(int i = 2 ; i<=N ; i++)
{
if(dist[i]==INF) g<<0<<' ';
else
g<<dist[i] <<' ';
}
cout<<endl;
return 0;
}