Pagini recente » Cod sursa (job #1990817) | Cod sursa (job #2247945) | Cod sursa (job #2805218) | Cod sursa (job #1756129) | Cod sursa (job #1889431)
#include <fstream>
#include<vector>
#include<queue>
#define MAX 50010
#define inf 1000000000
using namespace std;
ifstream fi("dijkstra.in");
ofstream fo("dijkstra.out");
vector <pair<int,int> > G[MAX];
priority_queue <pair<int,int>> PQ;
int dist[MAX],viz[MAX];
int n,m;
int main()
{
int x,y,c,i,nod;
fi>>n>>m;
while(m--)
{
fi>>x>>y>>c;
G[x].push_back(make_pair(y,c));
}
dist[1]=0;
for(i=2; i<=n; i++)
{
dist[i]=inf;
}
PQ.push(make_pair(0,1));
while(PQ.size())
{
pair<int,int>aux=PQ.top();
PQ.pop();
nod=aux.second;
if(viz[nod])
continue;
viz[nod] = 1;
for(auto it: G[nod])
if(dist[nod]+it.second<dist[it.first])
{
dist[it.first]=dist[nod]+it.second;
PQ.push(make_pair(-dist[it.first], it.first));
}
}
for(i=2; i<=n; i++)
{
if(dist[i]==inf)
dist[i]=0;
fo<<dist[i]<<" ";
}
fi.close();
fo.close();
}