Pagini recente » Cod sursa (job #1865457) | Cod sursa (job #309171) | Cod sursa (job #627179) | Cod sursa (job #565998) | Cod sursa (job #673461)
Cod sursa(job #673461)
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#define INF 1000000
struct djikstra_node {
djikstra_node(int v)
{
value = v;
distance = INF;
}
bool operator<(const djikstra_node &n) const
{
return distance < n.distance;
}
int value;
int distance;
};
class djikstra_compare
{
public:
bool operator() (const djikstra_node& lhs, const djikstra_node& rhs) const
{
return (lhs.distance > rhs.distance);
}
};
struct node {
int value;
int weight;
struct node *next;
};
struct list {
node *head;
};
list* create_list()
{
list *l = (list*)calloc(1, sizeof(list));
l->head = NULL;
return l;
}
void insert_list(list* l, int value, int weight)
{
if (l->head == NULL) {
l->head = (node*)calloc(1, sizeof(node));
l->head->value = value;
l->head->weight = weight;
return;
}
node *p = l->head;
while (p->next != NULL)
p = p->next;
node *n = (node*)calloc(1, sizeof(node));
n->value = value;
n->weight = weight;
p->next = n;
}
void destroy_list(list **l)
{
while ((*l)->head != NULL) {
node *p = (*l)->head;
(*l)->head = (*l)->head->next;
free(p);
}
}
enum colors {
WHITE = 0,
GRAY,
BLACK
};
struct graph {
int n;
list **adj;
int *colors;
int *parents;
djikstra_node *nodes;
};
graph* create_graph(int size)
{
graph *g = (graph*)calloc(1, sizeof(graph));
g->n = size;
g->adj = (list**)calloc(size, sizeof(list*));
g->colors = (int*)calloc(size, sizeof(int));
g->parents = (int*)calloc(size, sizeof(int));
g->nodes = (djikstra_node*)calloc(size, sizeof(djikstra_node));
for (int i = 0; i < size; i++)
g->nodes[i] = djikstra_node(i);
return g;
}
void add_graph_edge(graph *g, int v1, int v2, int w)
{
if (g->adj[v1] == NULL)
g->adj[v1] = create_list();
insert_list(g->adj[v1], v2, w);
}
void clear_graph_value(graph *g)
{
for (int i = 0; i < g->n; i++) {
g->colors[i] = WHITE;
g->parents[i] = -1;
}
}
void destroy_graph(graph **g)
{
for (int i = 0; i < (*g)->n; i++) {
if ((*g)->adj[i])
destroy_list(&((*g)->adj[i]));
}
free((*g)->adj);
free((*g)->colors);
free((*g)->parents);
free(*g);
}
void djikstra(graph *g)
{
std::priority_queue<djikstra_node, std::vector<djikstra_node>, djikstra_compare> q;
for (int i = 0; i < g->n; i++)
q.push(g->nodes[i]);
while (q.size()) {
djikstra_node n = q.top();
if (n.distance == INF)
break;
q.pop();
g->colors[n.value] = GRAY;
node *x = (g->adj[n.value]) ? g->adj[n.value]->head : NULL;
while (x) {
if (g->colors[x->value] == WHITE) {
int d = g->nodes[n.value].distance + x->weight;
if (d < g->nodes[x->value].distance) {
g->nodes[x->value].distance = d;
q.push(g->nodes[x->value]);
}
}
x = x->next;
}
}
}
int main()
{
int v, e;
graph * g = NULL;
freopen("djikstra.in", "r", stdin);
freopen("djikstra.out", "w", stdout);
scanf("%d %d\n", &v, &e);
g = create_graph(v);
for (int i = 0; i < e; i++) {
int v1, v2, w;
scanf("%d %d %d\n", &v1, &v2, &w);
add_graph_edge(g, v1 - 1, v2 - 1, w);
}
g->nodes[0].distance = 0;
clear_graph_value(g);
djikstra(g);
for (int i = 1; i < g->n; i++)
printf("%d ", g->nodes[i].distance == INF ? 0 : g->nodes[i].distance);
printf("\n");
destroy_graph(&g);
return 0;
}