Cod sursa(job #3249879)

Utilizator RaresPoinaruPoinaru-Rares-Aurel RaresPoinaru Data 18 octombrie 2024 17:24:29
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.05 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
#define visit kdjsfgj
#define int long long

const int MAXN=3e5+10;
const int INF=1e18;

int n,m;
vector <pair <int,int>> g[MAXN];
vector <int> G[MAXN];
priority_queue <pair <int,int>, vector <pair <int,int>>, greater <pair <int,int>>> pq;
int d1[MAXN],dn[MAXN];
pair <int,int> a[MAXN];


void dijsktra1 (){
    for (int i=1;i<=n;++i){
        d1[i]=INF;
    }
    d1[1]=0;
    pq.push ({0,1});
    while (!pq.empty ()){
        int node=pq.top ().second,cost=pq.top ().first;
        pq.pop ();

        if (cost!=d1[node]) continue;


        for (auto x:g[node]){
            int y=x.first,z=x.second;

            if (d1[y]>z+d1[node]){

                d1[y]=z+d1[node];
                pq.push ({d1[y],y});
            }
        }
    }
}
void dijsktran (){
    for (int i=1;i<=n;++i){
        dn[i]=INF;
    }
    dn[n]=0;
    pq.push ({0,n});
    while (!pq.empty ()){
        int node=pq.top ().second,cost=pq.top ().first;
        pq.pop ();
        if (cost!=dn[node]) continue;

        for (auto x:g[node]){
            int y=x.first,z=x.second;
            if (dn[y]>z+dn[node]){

                dn[y]=z+dn[node];
                pq.push ({y,dn[y]});
            }
        }
    }
}

bool ok[MAXN],visit[MAXN];
int tlow[MAXN],th[MAXN],t;
bool rez[MAXN];
void dfs (int node ,int prevn){
    //cout <<node<<' ';
    if (visit[node]) return;
    visit[node]=1;

    tlow[node]=th[node]=t++;

    for (auto aux:G[node]){
        int nextn=a[aux].first;
        if (nextn==node) nextn=a[aux].second;
        if (nextn==prevn) continue;
        if (visit[nextn]==0){
            dfs (nextn,node);
            tlow[node]=min (tlow[node],tlow[nextn]);
            if (tlow[nextn]>th[node]){
                rez[aux]=1;
            }
        }
        else{
            tlow[node]=min (tlow[node],th[nextn]);
            //ajung intr un nod deja parcurs
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio (false);
    cin.tie (nullptr);

    fin >>n>>m;
    for (int i=1;i<=m;++i){
        int x,y,z;
        fin >>x>>y>>z;
        a[i]={x,y};
        g[x].push_back ({y,z});
        g[y].push_back ({x,z});
    }
    dijsktra1 ();

    for (int i=2;i<=n;++i){
        if (d1[i]==INF) fout <<0<<' ';
        else
        fout <<d1[i]<<' ';
    }

    /*dijsktran ();

    int minn=d1[1]+dn[1];
    for (int i=1;i<=n;++i){

        if (d1[i]+dn[i]!=minn){
            ok[i]=1;
        }
    }
    //de acum contruiesc G
    for (int i=1;i<=m;++i){
        if (ok[a[i].first] || ok[a[i].second]) continue;

        G[a[i].first].push_back (i);
        G[a[i].second].push_back (i);
    }

    dfs (1,1);


    for (int i=1;i<=m;++i){

        if (rez[i]==0){
            cout <<"No"<<'\n';
        }
        else{
            cout <<"Yes"<<'\n';
        }
    }*/
    return 0;
}

/*
6 8
1 2 4
2 6 4
4 6 4
1 4 4
1 3 2
3 5 1
3 4 2
5 4 1
*/