Cod sursa(job #1521283)

Utilizator SirStevensIonut Morosan SirStevens Data 10 noiembrie 2015 08:41:52
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>

using namespace std;

#define Nmax std::numeric_limits<int>::max()

ifstream in("dijkstra.in");
ofstream out("dijkstra.out");

double n,m,c[10005][10005],c1,L[100005];
bool viz[100005];
deque <int> q;
vector <int> v[100005];

void Dijkstra()
{
    int node;
    while(!q.empty())
    {
        node=q.front();
        q.pop_front();

        for(int i=1;i<v[node].size()-1;i++)
        {
            if(!viz[v[node][i]]&&L[node]+c[node][i+1]<L[v[node][i]])
            {
                q.push_back(v[node][i]);
                viz[v[node][i]]=1;

                L[v[node][i]]=c[node][i]+L[node];
            }
        }
    }
}



int main()
{

    int x,y;
    in>>n>>m;
    for(int i=2;i<=n;i++)
        L[i]=Nmax;

    for(int i=1;i<=m;i++)
        {
            in>>x>>y>>c1;
            c[x][y]=c1;
            v[x].push_back(y);
        }
    L[1]=0;
    viz[1]=1;
    q.push_back(1);
    Dijkstra();
    for(int i=2;i<=n;i++)
        out<<L[i]<<" ";

    return 0;
}