#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
//#include "FibonacciHeap.h"
#define INF (1<<29) - 1
/*Copyright (c) 2010, Robin Message <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Univsersity of Cambridge nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF CAMBRIDGE OR ROBIN MESSAGE
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
typedef std::pair<int, int> edge;
class FibonacciHeap;
struct node {
private:
node* prev;
node* next;
node* child;
node* parent;
edge value;
int degree;
bool marked;
public:
friend class FibonacciHeap;
node* getPrev() {return prev;}
node* getNext() {return next;}
node* getChild() {return child;}
node* getParent() {return parent;}
edge getValue() {return value;}
bool isMarked() {return marked;}
bool hasChildren() {return child;}
bool hasParent() {return parent;}
};
class FibonacciHeap {
protected:
node* heap;
public:
FibonacciHeap() {
heap=_empty();
}
virtual ~FibonacciHeap() {
if(heap) {
_deleteAll(heap);
}
}
node* insert(edge value) {
node* ret=_singleton(value);
heap=_merge(heap,ret);
return ret;
}
void merge(FibonacciHeap& other) {
heap=_merge(heap,other.heap);
other.heap=_empty();
}
bool isEmpty() {
return heap==NULL;
}
edge getMinimum() {
return heap->value;
}
edge removeMinimum() {
node* old=heap;
heap=_removeMinimum(heap);
edge ret=old->value;
delete old;
return ret;
}
void decreaseKey(node* n,edge value) {
heap=_decreaseKey(heap,n,value);
}
node* find(edge value) {
return _find(heap,value);
}
private:
node* _empty() {
return NULL;
}
node* _singleton(edge value) {
node* n=new node;
n->value=value;
n->prev=n->next=n;
n->degree=0;
n->marked=false;
n->child=NULL;
n->parent=NULL;
return n;
}
node* _merge(node* a,node* b) {
if(a==NULL)return b;
if(b==NULL)return a;
if(a->value.second>b->value.second) {
node* temp=a;
a=b;
b=temp;
}
node* an=a->next;
node* bp=b->prev;
a->next=b;
b->prev=a;
an->prev=bp;
bp->next=an;
return a;
}
void _deleteAll(node* n) {
if(n!=NULL) {
node* c=n;
do {
node* d=c;
c=c->next;
_deleteAll(d->child);
delete d;
} while(c!=n);
}
}
void _addChild(node* parent,node* child) {
child->prev=child->next=child;
child->parent=parent;
parent->degree++;
parent->child=_merge(parent->child,child);
}
void _unMarkAndUnParentAll(node* n) {
if(n==NULL)return;
node* c=n;
do {
c->marked=false;
c->parent=NULL;
c=c->next;
}while(c!=n);
}
node* _removeMinimum(node* n) {
_unMarkAndUnParentAll(n->child);
if(n->next==n) {
n=n->child;
} else {
n->next->prev=n->prev;
n->prev->next=n->next;
n=_merge(n->next,n->child);
}
if(n==NULL)return n;
node* trees[64]={NULL};
while(true) {
if(trees[n->degree]!=NULL) {
node* t=trees[n->degree];
if(t==n)break;
trees[n->degree]=NULL;
if(n->value.second<t->value.second) {
t->prev->next=t->next;
t->next->prev=t->prev;
_addChild(n,t);
} else {
t->prev->next=t->next;
t->next->prev=t->prev;
if(n->next==n) {
t->next=t->prev=t;
_addChild(t,n);
n=t;
} else {
n->prev->next=t;
n->next->prev=t;
t->next=n->next;
t->prev=n->prev;
_addChild(t,n);
n=t;
}
}
continue;
} else {
trees[n->degree]=n;
}
n=n->next;
}
node* min=n;
node* start=n;
do {
if(n->value.second<min->value.second)min=n;
n=n->next;
} while(n!=start);
return min;
}
node* _cut(node* heap,node* n) {
if(n->next==n) {
n->parent->child=NULL;
} else {
n->next->prev=n->prev;
n->prev->next=n->next;
n->parent->child=n->next;
}
n->next=n->prev=n;
n->marked=false;
return _merge(heap,n);
}
node* _decreaseKey(node* heap,node* n,edge value) {
if(n->value.second<value.second)return heap;
n->value=value;
if(n->parent) {
if(n->value.second<n->parent->value.second) {
heap=_cut(heap,n);
node* parent=n->parent;
n->parent=NULL;
while(parent!=NULL && parent->marked) {
heap=_cut(heap,parent);
n=parent;
parent=n->parent;
n->parent=NULL;
}
if(parent!=NULL && parent->parent!=NULL)parent->marked=true;
}
} else {
if(n->value.second < heap->value.second) {
heap = n;
}
}
return heap;
}
node* _find(node* heap,edge value) {
node* n=heap;
if(n==NULL)return NULL;
do {
if(n->value.second==value.second)return n;
node* ret=_find(n->child,value);
if(ret)return ret;
n=n->next;
}while(n!=heap);
return NULL;
}
};
typedef std::pair<int, int> edge;
class comparaPrioritati {
public:
bool operator()(edge const &p1, edge const &p2)
{
if (p1.second > p2.second)
return true;
return false;
}
};
std::vector<int> shortest_path_Dijkstra(const std::vector<std::vector<edge> >& graph, const int source) {
std::priority_queue <edge, std::vector<edge>, comparaPrioritati> coadaDePrioritati;
std::vector<int> dist(graph.size(), INF);
std::vector<int> viz(graph.size(), 0);
dist[source] = 0;
edge act;
int aux, lungimeNoua, nodDestinatie;
unsigned int i;
coadaDePrioritati.push(std::make_pair(source, 0));
while (!coadaDePrioritati.empty()) {
act = coadaDePrioritati.top();
viz[act.first] = 1;
for (i = 0; i < graph[act.first].size(); i++) {
lungimeNoua = graph[act.first][i].second;
nodDestinatie = graph[act.first][i].first;
if (lungimeNoua != INF && viz[nodDestinatie] == 0) {
aux = act.second + lungimeNoua;
if (aux < dist[nodDestinatie]) {
dist[nodDestinatie] = aux;
coadaDePrioritati.push(std::make_pair(nodDestinatie, aux));
}
}
}
coadaDePrioritati.pop();
}
return dist;
}
std::vector<int> shortest_path_Dijkstra_FiboHeap(const std::vector<std::vector<edge> >& graph, const int source) {
FibonacciHeap coadaDePrioritati;
std::vector<int> dist(graph.size(), INF);
std::vector<int> viz(graph.size(), 0);
dist[source] = 0;
edge act;
int aux, lungimeNoua, nodDestinatie;
unsigned int i;
coadaDePrioritati.insert(std::make_pair(source, 0));
while (!coadaDePrioritati.isEmpty()) {
act = coadaDePrioritati.getMinimum();
viz[act.first] = 1;
for (i = 0; i < graph[act.first].size(); i++) {
lungimeNoua = graph[act.first][i].second;
nodDestinatie = graph[act.first][i].first;
if (lungimeNoua != INF && viz[nodDestinatie] == 0) {
aux = act.second + lungimeNoua;
if (aux < dist[nodDestinatie]) {
dist[nodDestinatie] = aux;
coadaDePrioritati.insert(std::make_pair(nodDestinatie, aux));
}
}
}
coadaDePrioritati.removeMinimum();
}
return dist;
}
std::vector<int> shortest_path_BellmanFord(const std::vector<std::vector<edge> >& graph, const int source) {
std::vector<int> dist(graph.size(), INF);
std::vector<bool> inQueue(graph.size(), false);
dist[source] = 0;
std::queue<int> coada;
coada.push(source);
int curr, lungimeNoua, nodDestinatie;
while (!coada.empty()) {
curr = coada.front();
inQueue[curr] = false;
coada.pop();
for (unsigned int k = 0; k < graph[curr].size(); k++) {
lungimeNoua = graph[curr][k].second;
nodDestinatie = graph[curr][k].first;
if (dist[curr] + lungimeNoua < dist[nodDestinatie]) {
dist[nodDestinatie] = dist[curr] + lungimeNoua;
if (!inQueue[nodDestinatie]) {
inQueue[nodDestinatie] = true;
coada.push(nodDestinatie);
}
}
}
}
for (unsigned int j = 0; j < graph.size(); j++) {
for (unsigned int k = 0; k < graph[j].size(); k++) {
if (dist[j] + graph[j][k].second < dist[graph[j][k].first]) {
printf("EROARE graful contine cicluri negative");
return std::vector<int>();
}
}
}
return dist;
}
FILE *f = fopen("dijkstra.in", "r");
FILE *g = fopen("dijkstra.out", "w");
int main()
{
int n, m, start;
fscanf(f, "%d %d", &n, &m);
std::vector<std::vector<edge> > graf(n + 1);
int from, to, cost;
for (int i = 0; i < m; i++) {
fscanf(f, "%d %d %d", &from, &to, &cost);
graf[from].push_back(std::make_pair(to, cost));
}
std::vector<int> dist = shortest_path_Dijkstra_FiboHeap(graf, 1);
for (int i = 1; i < dist.size(); i++) {
if (i != 1)
fprintf(g, "%d ", dist[i] != INF ? dist[i] : 0);
}
return 0;
}