Cod sursa(job #2702339)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 3 februarie 2021 18:22:08
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.46 kb
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
#define debug(v,n) for (int i = 1; i <= (n); ++i) cout << v[i] << " ";
#define next cout << '\n'
using namespace std;

const int N = 30005;
vector<pair<int,int>> graf[N];
int n, m, x, y;
bool drum[N][N], in[N];

struct cmp {
    bool operator()(int a, int b) {
        return sz(graf[a]) < sz(graf[b]);
    }
};
priority_queue<int, vector<int>, cmp> coada;

int main() {
    //ifstream fin("date.in.txt");
    ifstream fin("sate.in");
    ofstream fout("sate.out");
    fin >> n >> m >> x >> y;

    for (int i = 1; i <= m; ++i) {
        int a, b, d;
        fin >> a >> b >> d;
        graf[a].push_back({b, d});
        graf[b].push_back({a, d});
        drum[a][b] = true;
        drum[b][a] = true;
    }
    for (int i = x; i <= y; ++i) {
        coada.push(i);
        in[i] = true;
    }

    while(!coada.empty()) {
        int nod = coada.top();
        //cout << nod << " : \n";
        coada.pop();
        in[nod] = false;

        for (int i = 0; i < sz(graf[nod]); ++i) {
            int act = graf[nod][i].first, lgAct = graf[nod][i].second;

            if(act > y || act < nod)
                continue;

            // [nod act] sau [act to]
            for (int j = i + 1; j < sz(graf[nod]); ++j) {
                int to = graf[nod][j].first, lg = graf[nod][j].second;

                if(to > y || to < act)
                    continue;

                if(act < to && !drum[act][to]) { // [act to] = [nod to] - [nod act]
                    graf[act].push_back({to, lg - lgAct});
                    graf[to].push_back({act, lg - lgAct});

                    drum[act][to] = true;
                    drum[to][act] = true;
                    cout << act << " " << to << " " << lg - lgAct << " drum nou gasit1 \n";

                    if(in[to] == false) {
                        coada.push(to);
                        in[to] = true;
                    }
                }
                else if(!drum[act][to]) { // act > to  => [to act] = [nod act] - [nod to]
                    graf[act].push_back({to, lgAct - lg});
                    graf[to].push_back({act, lgAct - lg});

                    drum[act][to] = true;
                    drum[to][act] = true;
                    //cout << act << " " << to << " " << lgAct - lg << " drum nou gasit1 \n";

                    if(in[to] == false) {
                        coada.push(to);
                        in[to] = true;
                    }
                }
            }
            // [nod, act] + [act, to];

            for (auto& [to, lg] : graf[act]) {
                if(to < act || to > y)
                    continue;

                if(!drum[nod][to]) {
                    graf[nod].push_back({to, lgAct + lg});
                    graf[to].push_back({nod, lgAct + lg});

                    drum[nod][to] = true;
                    drum[to][nod] = true;
                    //cout << nod << " " << to << " " << lg + lgAct << " drum nou gasit \n";

                    if(in[to] == false) {
                        coada.push(to);
                        in[to] = true;
                    }
                }
            }


        }
        //cout << '\n';
    }
    for (int i = 0; i < sz(graf[x]); ++i) {
        if(graf[x][i].first == y) {
            fout << graf[x][i].second;
            return 0;
        }
    }

    return 0;
}