Cod sursa(job #2739113)

Utilizator MoldovanAndrei1Moldovan Andrei MoldovanAndrei1 Data 6 aprilie 2021 20:47:56
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>
using namespace std;
const int NMAX=1e5;
struct edge
{
    int x, cost;
}e;
vector<edge>v[NMAX];
int dp[NMAX + 5];
bitset<NMAX+5>fr;
int main()
{
    freopen("dijkstra.in","r",stdin);
    freopen("dijkstra.out","w",stdout);
    int n , m , i , j , k , a, b,c;
    scanf("%d%d",&n,&m);
    for(i = 1 ; i <= m ;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        e.x=b;e.cost=c;
        v[a].push_back(e);
    }
    for(i=2;i<=n;i++)dp[i]=NMAX*100;
    dp[1] = 0;
    queue<int>q;
    q.push(1);
    fr[1] = 1;
    while(!q.empty())
    {
        int nod = q.front();
        q.pop();
        fr[nod] = 0;
        for(auto it : v[nod])
        {
            if(dp[it.x] > dp[nod] + it.cost)
            {
                dp[it.x] = dp[nod] + it.cost;
                if(!fr[it.x])
                    q.push(it.x);
                fr[it.x]=1;
            }
        }
    }
    for(i = 2 ; i <= n ;i++)
        printf("%d ",dp[i]);
    return 0;
}