Pagini recente » Cod sursa (job #2351646) | Cod sursa (job #1650346) | Cod sursa (job #636715) | Cod sursa (job #2117764) | Cod sursa (job #639770)
Cod sursa(job #639770)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#define NMAX 50002
#define oo 99999
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int D[NMAX],N,M;
class cmp
{
public:
inline bool operator()(const int&a,const int&b){return D[a]>D[b];}
};
vector<pair<int,int> > V[NMAX];
vector<pair<int,int> >::iterator it,sf;
priority_queue<int,vector<int>,cmp> Heap;
//bitset<NMAX> In_Heap;
bool In_Heap[NMAX];
int main()
{
int x,y,c;
in>>N>>M;
while(M--)
{
in>>x>>y>>c;
V[x].push_back(make_pair(y,c));
}
//plec din nodul 1
fill(D+2,D+N+1,oo);
for(Heap.push(1);!Heap.empty();Heap.pop())
{
x = Heap.top();
In_Heap[x] = 0;
for(it=V[x].begin(),sf=V[x].end();it<sf;++it)
{
y = it->first;
c = it->second;
if(D[y]>D[x]+c)
{
D[y] = D[x]+c;
if(!In_Heap[y])
Heap.push(y),In_Heap[y] = 1;
}
}
}
for(x=2;x<=N;++x)out<<(D[x]==oo?0:D[x])<<' ';
return 0;
}