Pagini recente » Cod sursa (job #2389306) | Cod sursa (job #2799840) | Cod sursa (job #1838320) | Cod sursa (job #2220352) | Cod sursa (job #491363)
Cod sursa(job #491363)
#include<fstream>
#include<iostream>
#include<vector>
using namespace std;
#define INF 0x17D7841
#define MAXN 50001
typedef vector<vector<pair<unsigned short,int> > > Graph;
class CHeap
{
private:
int size;
vector<int> vIndexes;
vector<pair<int,unsigned short> > vHeap;
inline void swap(const int first, const int second)
{
vIndexes[vHeap[first].second]=second;
vIndexes[vHeap[second].second]=first;
pair<int,unsigned short> aux;
aux=vHeap[first];
vHeap[first]=vHeap[second];
vHeap[second]=aux;
}
void siftUp(int index)
{
int parent=(index-1-!(index%2))>>1;
while(parent>=0 && vHeap[index].first<vHeap[parent].first)
{
swap(parent,index);
index=parent;
parent=(index-1-!(index%2))>>1;
}
}
void siftDown(int index)
{
int child1,child2;
bool ord=true;
child1=(index<<1)+1;
child2=(index+1)<<1;
while(ord && child1<size)
{
ord=false;
if(child1<size && child2<size)
{
if(vHeap[child1].first,vHeap[child2].first)
{
if(vHeap[child1]<vHeap[index])
{
swap(child1,index);
index=child1;
ord=true;
}
}
else if(vHeap[child2].first<vHeap[index].first)
{
swap(child2,index);
index=child2;
ord=true;
}
}
else if(child1<size)
{
if(vHeap[child1].first<vHeap[index].first)
{
swap(child1,index);
index=child1;
ord=true;
}
}
child1=(index<<1)+1;
child2=(index+1)<<1;
}
}
public:
CHeap() : size(0)
{}
void insert(const pair<int,unsigned short>& val)
{
vHeap.push_back(val);
size++;
vIndexes.push_back(size-1);
siftUp(size-1);
}
void modify(const int pos, const pair<int,unsigned short>& val)
{
if(vHeap[pos].first<val.first)
vHeap[pos]=val;
else
vHeap[pos]=val;
siftUp(pos);
}
void remove(const int pos)
{
swap(pos,--size);
vHeap.pop_back();
siftDown(pos);
}
const pair<int,unsigned short>& getElement(const int pos) const
{
return vHeap[pos];
}
int getPos(const int time) const
{
return vIndexes[time];
}
bool empty() const
{
return (size==0);
}
void print()
{
for(unsigned int i=0; i<vHeap.size(); ++i)
{
cout<<"("<<vHeap[i].first<<" "<<vHeap[i].second<<") ";
}
cout<<endl;
}
};
void Dijkstra(Graph& graph, const int n, const unsigned short s, int* dist)
{
CHeap heap;
dist[s]=0;
heap.insert(make_pair(0,s));
for(int i=1; i<n; ++i)
{
/*if(i==s)
{
heap.insert(make_pair(0,i));
dist[i]=0;
}
else*/
{
heap.insert(make_pair(INF,i));
dist[i]=INF;
}
}
//heap.print();
while(!heap.empty())
{
const pair<int,unsigned short> node=heap.getElement(0);
dist[node.second]=node.first;
heap.remove(0);
if(node.first==INF)
break;
for(int i=0; i<(int)graph[node.second].size(); ++i)
{
const int alt=dist[node.second]+graph[node.second][i].second;
if(alt<dist[graph[node.second][i].first])
{
dist[graph[node.second][i].first]=alt;
heap.modify(heap.getPos(graph[node.second][i].first),make_pair(alt,graph[node.second][i].first));
}
}
}
}
int main()
{
int n,m,a,b,c,*dist;
fstream fin("dijkstra.in", fstream::in);
fstream fout("dijkstra.out", fstream::out);
Graph graph;
fin>>n>>m;
graph.resize(n+2);
dist=new int[n+2];
for(int i=0; i<m; ++i)
{
fin>>a>>b>>c;
//cout<<a<<" "<<b<<" "<<c<<"\n";
graph[a-1].push_back(make_pair(b-1,c));
}
Dijkstra(graph,n,0,dist);
for(int i=1; i<n; ++i)
{
if(dist[i]==INF)
fout<<0<<" ";
else
fout<<dist[i]<<" ";
/*switch(dist[i])
{
case INF:
fout<<0<<" ";
break;
default:
fout<<dist[i]<<" ";
}*/
}
fout<<endl;
/*for(int i=0; i<5; ++i)
heap.insert(make_pair(5-i,i));
heap.print();
heap.modify(3,make_pair(-1,0));
heap.print();
heap.remove(heap.getPos(0));
heap.print();
heap.remove(heap.getPos(3));
heap.print();*/
//delete dist;
fin.close();
fout.close();
return 0;
}