Pagini recente » Cod sursa (job #2874766) | Cod sursa (job #178875) | Cod sursa (job #623812) | Cod sursa (job #3178259) | Cod sursa (job #1299947)
#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;
void Dijkstra(nod s)
{
D[s.b]=0;
PQ.push(s);
while(!PQ.empty())
{
nod x=PQ.top();
PQ.pop();
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;
PQ.push(G[x.b][i]);
}
}
}
}
int main()
{
f>>n>>m;
G.resize(n+1);
for(int i=0; i<=n; i++)
{
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();
//fara INPQ
return 0;
}