0% found this document useful (0 votes)
46 views4 pages

Cerinta: Dijkstra - in Dijkstra - Out

The document describes an algorithm to find the shortest path from node 1 to all other nodes in a graph. It takes as input a graph with N nodes and M edges, represented by the nodes and length of each edge. It uses Dijkstra's algorithm implemented with a heap to find the minimum distance from node 1 to all other nodes 2 through N, and outputs these distances.

Uploaded by

Adascalitei Ioan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Cerinta: Dijkstra - in Dijkstra - Out

The document describes an algorithm to find the shortest path from node 1 to all other nodes in a graph. It takes as input a graph with N nodes and M edges, represented by the nodes and length of each edge. It uses Dijkstra's algorithm implemented with a heap to find the minimum distance from node 1 to all other nodes 2 through N, and outputs these distances.

Uploaded by

Adascalitei Ioan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

e da un graf orientat cu N noduri si M arce.

Cerinta
Sa se determine lungimea minima a drumului de la nodul 1 la fiecare din nodurile 2, 3, ..., N1, N si sa se afiseze aceste lungimi. Lungimea unui drum este data de suma lungimilor arcelor care
constituie drumul.

Date de intrare
Fisierul de intrare dijkstra.in contine pe prima linie numerele N si M, separate printr-un spatiu,
cu semnificatia din enunt. Urmatoarele M linii contin, fiecare, cate 3 numere naturale separate
printr-un spatiu A B C semnificand existenta unui arc de lungime C de la nodul A la nodul B.

Date de iesire
In fisierul de iesire dijkstra.out veti afisa pe prima linie N-1 numere naturale separate printr-un
spatiu. Al i-lea numar va reprezenta lungimea unui drum minim de la nodul 1 la nodul i+1.

Restrictii

1 N 50 000
1 M 250 000
Lungimile arcelor sunt numere naturale cel mult egale cu 1 000.
Pot exista arce de lungime 0
Nu exista un arc de la un nod la acelasi nod.
Daca nu exista drum de la nodul 1 la un nod i, se considera ca lungimea minima este 0.
Arcele date in fisierul de intrare nu se repeta.

Exemplu
dijkstra.in
5 6
1 2 1
1 4 2
4 3 4
2 3 2
4 5 3
3 5 6

dijkstra.out
1 3 2 5

#include <cstdio>
002.
003.const int maxn = 50001;
004.const int inf = 1 << 30;
005.
006.FILE *in = fopen("dijkstra.in","r"), *out = fopen("dijkstra.out","w");
007.
008.struct graf
009.{
010.int nod, cost;
011.graf *next;
012.};
013.
014.int n, m;
015.graf *a[maxn];
016.int d[maxn], h[maxn], poz[maxn], k;
017.
018.void add(int where, int what, int cost)
019.{
020.graf *q = new graf;
021.q->nod = what;
022.q->cost = cost;
023.q->next = a[where];
024.a[where] = q;
025.}
026.
027.void read()
028.{
029.fscanf(in, "%d %d", &n, &m);
030.
031.int x, y, z;
032.for ( int i = 1; i <= m; ++i )
033.{
034.fscanf(in, "%d %d %d", &x, &y, &z);
035.add(x, y, z);
036.}
037.}
038.
039.void swap(int x, int y)
040.{
041.int t = h[x];
042.h[x] = h[y];
043.h[y] = t;
044.}
045.
046.void upheap(int what)
047.{
048.int tata;
049.while ( what > 1 )
050.{
051.tata = what >> 1;
052.
053.if ( d[ h[tata] ] > d[ h[what] ] )
054.{
055.poz[ h[what] ] = tata;
056.poz[ h[tata] ] = what;
057.
058.swap(tata, what);
059.
060.what = tata;

061.}
062.else
063.what = 1;
064.}
065.}
066.
067.void downheap(int what)
068.{
069.int f;
070.while ( what <= k )
071.{
072.f = what;
073.if ( (what<<1) <= k )
074.{
075.f = what << 1;
076.if ( f + 1 <= k )
077.if ( d[ h[f + 1] ] < d[ h[f] ] )
078.++f;
079.}
080.else
081.return;
082.
083.if ( d[ h[what] ] > d[ h[f] ] )
084.{
085.poz[ h[what] ] = f;
086.poz[ h[f] ] = what;
087.
088.swap(what, f);
089.
090.what = f;
091.}
092.else
093.return;
094.}
095.}
096.
097.void dijkstra_heap()
098.{
099.for ( int i = 2; i <= n; ++i )
100.d[i] = inf, poz[i] = -1;
101.poz[1] = 1;
102.
103.h[++k] = 1;
104.
105.while ( k )
106.{
107.int min = h[1];
108.swap(1, k);
109.poz[ h[1] ] = 1;
110.--k;
111.
112.downheap(1);
113.
114.graf *q = a[min];
115.
116.while ( q )
117.{
118.if ( d[q->nod] > d[min] + q->cost )
119.{
120.d[q->nod] = d[min] + q->cost;
121.

122.if ( poz[q->nod] != -1 )
123.upheap( poz[q->nod] );
124.else
125.{
126.h[++k] = q->nod;
127.poz[ h[k] ] = k;
128.upheap( k );
129.}
130.}
131.q = q->next;
132.}
133.}
.}
.
.int main()
.{
.read();
.dijkstra_heap();
140.
.for ( int i = 2; i <= n; ++i )
.fprintf(out, "%d ", d[i] == inf ? 0 : d[i]);
.fprintf(out, "\n");
.return 0;
.}

You might also like