Pagini recente » Cod sursa (job #1596670) | Cod sursa (job #2751440) | Cod sursa (job #2000172) | Autentificare | Cod sursa (job #2160340)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define maxx 50002
#define cost first
#define nod second
#define inf 0x3f3f3f3f
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,j,c,i,viz[maxx];
vector < pair <int,int> > G[maxx];
vector < pair <int,int> > :: iterator it;
priority_queue < pair < int, int >, vector < pair < int, int > >, greater < pair < int, int > > > pq;
pair <int, int> aux;
int main()
{
f>>n>>m;
while(f>>i>>j>>c)
G[i].push_back(make_pair(c,j));
pq.push(make_pair(0,1));
while(!pq.empty())
{
aux=pq.top();
pq.pop();
if(!viz[aux.nod])
{
viz[aux.nod]=aux.cost;
for(it=G[aux.nod].begin();it!=G[aux.nod].end();it++)
{
if(!viz[(*it).nod])
pq.push(make_pair(aux.cost+(*it).cost,(*it).nod));
}
}
}
for(i=2;i<=n;i++)
{
g<<viz[i]<<" ";
}
f.close();
g.close();
return 0;
}