154242025-02-19 14:18:58AblablablaAutópálya inflációcpp17Wrong answer 8/1002.098s71732 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll, ll> pii;
typedef pair<ll, bool> pib;

const ll INF = 4e18 + 7;
const ll MOD = 1e9 + 7;

vector<int> minInd;
vector<vector<int>> elozo, el;


pib kulonbseg(int egy, int ket, int ido1, int ido2){ // kulonbseg, tort-e
    if(ido1 == ido2){
        int ido = ido1;
        if(ido == 0){
            return {0, 0};
        }

        auto [kul, tort] = kulonbseg(elozo[egy][ido], elozo[ket][ido], ido - 1, ido - 1);

        bool lesz = (tort || kul % 2 == 1);
        kul /= 2;

        int elso = el[egy][ido] + kul;
        int masodik = el[ket][ido];

        return {elso - masodik, lesz};
    } else{
        if(ido1 < ido2){
            auto [kul, tort] = kulonbseg(egy, elozo[ket][ido2], ido1, ido2 - 1);

            bool lesz = (tort || kul % 2 == 1);
            kul /= 2;

            int elso = kul;
            int masodik = el[ket][ido2];

            return {elso - masodik, lesz};
        } else{
            auto [kul, tort] = kulonbseg(elozo[egy][ido1], ket, ido1 - 1, ido2);

            bool lesz = (tort || kul % 2 == 1);
            kul /= 2;

            int elso = el[egy][ido1] + kul;
            int masodik = 0;

            return {elso - masodik, lesz};
        }
    }
}

bool kisebb(int egy, int ket, int ido1, int ido2){
    auto [kul, tort] = kulonbseg(egy, ket, ido1, ido2);

    //cout << egy << " " << ido1 << " : " << ket << " " << ido2 << "\n";
    //cout << "kul, tort: " << kul << " " << tort << "\n";

    if(kul == 0){
        return tort;
    } else{
        return (kul < 0);
    }
}

bool kisebb(int egy, int ket, int ido1, int ido2, int lenne){
    auto [kul, tort] = kulonbseg(egy, elozo[ket][ido2], ido1, ido1);

    bool lesz = (tort || kul % 2 == 1);
    //cout << "kul, tort: " << kul << " " << tort << " :lesz: " << lesz << "\n";
    kul /= 2;

    int elso = lenne + kul;
    int masodik = el[ket][ido2];

    //cout << "elso, masodik: " << elso << " " << masodik << "\n";

    if(elso - masodik != 0){
        return (elso - masodik < 0);
    } else{
        return lesz;
    }
}

struct comp{
    bool operator()(ll a, ll b){
        //cout << "\n\nsort miatt\n";
        bool c = !kisebb(a, b, minInd[a], minInd[b]);
        //cout << "vege\n\n";
        return c;
    }
};

ll hatvany(int a){
    a--;
    ll vissza = 1;
    ll szor = 2;

    for(int i = 0; i < 31; i++){
        if(a & (1 << i)){
            vissza *= szor;
            vissza %= MOD;
        }

        szor *= szor;
        szor %= MOD;
    }

    return vissza;
}

ll leker(int akt, int ido){
    if(ido == 0){
        return 0;
    }

    ll a = leker(elozo[akt][ido], ido - 1);
    ll b = (hatvany(ido)*el[akt][ido])%MOD;
    ////cout << "leker: " << akt << " " << ido << "   ment: " << elozo[akt][ido] << " " << ido - 1 << " :: " << a << " el miatt: " << b << "\n";
    return (b + a) % MOD;
}

int main()
{
    ll n, m;
    cin >> n >> m;

    vector<vector<ll>> csucsok(n, vector<ll>()), hossz(n, vector<ll>());
    for(ll i = 0; i < m; i++){
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;

        csucsok[a].push_back(b);
        hossz[a].push_back(c);
        csucsok[b].push_back(a);
        hossz[b].push_back(c);
    }

    priority_queue<ll, vector<ll>, comp> bejar;
    bejar.push(0);
    vector<bool> bejart(n);
    elozo.assign(n, vector<int>(n + 2, -1));
    el.assign(n, vector<int>(n + 2, -1));
    minInd.assign(n, -1);
    elozo[0][0] = 0;
    el[0][0] = 0;
    minInd[0] = 0;


    while(!bejar.empty()){
        ll akt = bejar.top();
        bejar.pop();

        if(bejart[akt]) continue;

        bejart[akt] = 1;

        for(ll i = 0; i < csucsok[akt].size(); i++){
            ll kovi = csucsok[akt][i];
            ll suly = hossz[akt][i];

            if(bejart[kovi]) continue;

            for(int i = 0; i <= n; i++){
                if(el[akt][i] == -1) continue;

                if(elozo[kovi][i + 1] == -1){
                    el[kovi][i + 1] = suly;
                    elozo[kovi][i + 1] = akt;

                    if(minInd[kovi] == -1){
                        minInd[kovi] = i + 1;
                    } else{
                        //cout << "\n\nminInd miatt:\n";
                        bool b = kisebb(kovi, kovi, i + 1, minInd[kovi]);
                        //cout << "vege\n\n";

                        if(b){
                            minInd[kovi] = i + 1;
                        }
                    }

                } else{
                    //cout << "\n\nkisebb kezd\n";
                    //cout << akt << " : " << kovi << " " << i << "\n";
                    bool a = kisebb(akt, kovi, i, i + 1, suly);
                    //cout << "kisebb veg\n\n\n";


                    if(a){
                        el[kovi][i + 1] = suly;
                        elozo[kovi][i + 1] = akt;

                        if(minInd[kovi] == -1){
                            minInd[kovi] = i + 1;
                        } else{
                            bool b = kisebb(kovi, kovi, i + 1, minInd[kovi]);

                            if(b){
                                minInd[kovi] = i + 1;
                            }
                        }
                    }
                }
            }

            bejar.push(kovi);
        }
    }

    for(int i = 1; i < n; i++){
        cout << leker(i, minInd[i]) << "\n";
    }

    /*cout << "\nel:\n";
    for(int i = 0; i <= n; i++){
        for(int j = 0; j < n; j++){
            cout << el[j][i] << " ";
        }
        cout << "\n";
    }
    cout << "\nelozo:\n";
    for(int i = 0; i <= n; i++){
        for(int j = 0; j < n; j++){
            cout << elozo[j][i] << " ";
        }
        cout << "\n";
    }
    cout << "\nminInd:\n";
    for(int i = 0; i < n; i++){
        cout << minInd[i] << " ";
    }
    cout << "\n";*/
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Wrong answer195ms71476 KiB
subtask28/8
3Accepted97ms71476 KiB
4Accepted144ms71476 KiB
5Accepted196ms71256 KiB
6Accepted76ms71476 KiB
7Accepted1.164s71476 KiB
subtask30/15
8Accepted1ms316 KiB
9Wrong answer1ms316 KiB
10Accepted1ms316 KiB
11Accepted1ms508 KiB
12Accepted1ms316 KiB
13Accepted1ms368 KiB
14Accepted1ms316 KiB
15Accepted1ms316 KiB
16Accepted1ms316 KiB
17Accepted1ms316 KiB
18Accepted1ms316 KiB
subtask40/34
19Wrong answer108ms71484 KiB
20Wrong answer552ms71456 KiB
21Wrong answer483ms71652 KiB
22Wrong answer162ms71552 KiB
23Wrong answer130ms71476 KiB
24Time limit exceeded2.081s71480 KiB
25Wrong answer96ms71476 KiB
26Wrong answer92ms71476 KiB
27Time limit exceeded2.086s71476 KiB
28Wrong answer211ms71088 KiB
29Wrong answer273ms71220 KiB
30Wrong answer586ms62260 KiB
31Accepted246ms70964 KiB
32Wrong answer703ms71040 KiB
33Wrong answer704ms70964 KiB
subtask50/21
34Accepted199ms2552 KiB
35Wrong answer86ms2356 KiB
36Wrong answer9ms2356 KiB
37Wrong answer10ms2556 KiB
38Accepted8ms2476 KiB
39Accepted8ms2568 KiB
40Time limit exceeded2.098s2500 KiB
41Time limit exceeded2.098s2500 KiB
42Wrong answer8ms2356 KiB
43Accepted17ms2356 KiB
44Accepted17ms2356 KiB
45Accepted17ms2356 KiB
46Accepted13ms2248 KiB
47Accepted12ms2292 KiB
48Wrong answer17ms2356 KiB
49Wrong answer18ms2356 KiB
subtask60/22
50Time limit exceeded2.085s71480 KiB
51Wrong answer279ms71436 KiB
52Wrong answer153ms71460 KiB
53Wrong answer112ms71668 KiB
54Accepted94ms71668 KiB
55Accepted108ms71732 KiB
56Time limit exceeded2.082s71476 KiB
57Time limit exceeded2.091s71476 KiB
58Wrong answer74ms8244 KiB
59Wrong answer254ms71220 KiB
60Wrong answer293ms71220 KiB
61Wrong answer352ms71256 KiB
62Accepted465ms70072 KiB
63Accepted225ms70196 KiB
64Wrong answer1.503s71168 KiB
65Wrong answer1.669s70976 KiB
66Accepted1.281s71476 KiB