Pagini recente » Cod sursa (job #776491) | Cod sursa (job #1728764) | Cod sursa (job #2020032) | Cod sursa (job #1134536) | Cod sursa (job #1299944)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define inf (1<<30)
using namespace std;
int n,m,x,y,c;
ifstream f("dijkstra.in");
ofstream go("dijkstra.out");
struct nod
{
int b,cost;
}aux;
struct cmp
{
bool operator()(const nod& a, const nod& b) const
{
return (a.cost > b.cost);
}
};
priority_queue<nod, vector<nod>, cmp> PQ;
vector<vector<nod> > G;
vector<int> D;
vector<bool> INPQ;
void Dijkstra(nod s)
{
D[s.b]=0;
PQ.push(s);
INPQ[s.b]=true;
while(!PQ.empty())
{
nod x=PQ.top();
PQ.pop();
INPQ[x.b]=false;
for(int i=0; i<G[x.b].size(); i++)
{
y=G[x.b][i].b;
c=G[x.b][i].cost;
if(D[y]>D[x.b]+c)
{
D[y]=D[x.b]+c;
if(!INPQ[y])
{
PQ.push(G[x.b][i]);
INPQ[y]=true;
}
}
}
}
}
int main()
{
f>>n>>m;
G.resize(n+1);
for(int i=0; i<=n; i++)
{
INPQ.push_back(false);
D.push_back(inf);
}
for(int i=0; i<m; i++)
{
f>>x>>y>>c;
aux.b=y;
aux.cost=c;
G[x].push_back(aux);
}
aux.b=1; //de chestie
aux.cost=1;
Dijkstra(aux);
for(int i=2; i<=n; i++)
{
if(D[i]!=inf)
go<<D[i]<<" ";
else
go<<"0 ";
}
f.close();
go.close();
return 0;
}