53962023-05-09 14:12:52mraronAutópálya inflációcpp17Accepted 100/1001.291s7484 KiB
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cassert>
#include<array>
using namespace std;
using ll = long long;
#define xx first
#define yy second

const int MAXN=3001;
const int mod=1e9+7;

int n,m;
vector<pair<int,int>> adj[MAXN];
vector<array<int, 4>> par[MAXN];

int get_len(int x, int y) {
    int res=0;
    while(x!=par[x][y][0]) {
        array<int, 4> p=par[x][y];
        x=p[0];
        y=p[1];
        res++;
    }
    return res;
}

ll compare(int a, int b, int w1, int l1,  int x, int y, int w2, int l2) {
    ll bal=0;
    int i=l1, j=l2;
    while(i>0 || j>0) {
        array<int, 4> p1=par[a][b],
                      p2=par[x][y];
        if(i>j) {
            bal=(bal-w1)*2-p1[2];
            w1=0;
            a=p1[0];
            b=p1[1];
            i--;
        }else if(i<j) {
            bal=(bal+w2)*2+p2[2];
            w2=0;
            x=p2[0];
            y=p2[1];
            j--;
        }else {
            bal=(bal-w1+w2)*2-p1[2]+p2[2];
            w1=w2=0;
            a=p1[0];
            b=p1[1];
            x=p2[0];
            y=p2[1];
            i--;j--;
        }

        if(abs(bal)>(ll)2e9) {
            return bal;
        }
    }
    bal-=w1;bal+=w2;
    return bal;
}

int get_ans(int x, int y) {
    int res=0;
    while(x!=par[x][y][0]) {
        array<int, 4> p=par[x][y];
        res=(res*2%mod+p[2])%mod;
        x=p[0];
        y=p[1];
    }

    return res;
}

struct state {
    int x;
    int pa, pb, pw;
    int len;

    bool operator<(const state& masik) const {
        ll b=compare(pa, pb, pw, len, masik.pa, masik.pb, masik.pw, masik.len);
        if(b>0) return true;
        if(b<0) return false;
        return len<masik.len;
    }
    bool operator>(const state& masik) const {
        return !(*this<masik);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;++i) {
        int a,b,c;
        cin>>a>>b>>c;
        adj[a].push_back({b,c});
        adj[b].push_back({a,c});
    }

    vector<int> ans(n+1, -1);
    priority_queue<state, vector<state>, greater<state>> pq;
    pq.push(state{1, 1, 0, 0, 0});
    while(!pq.empty()) {
        state curr=pq.top();
        pq.pop();
        //cerr<<curr.x<<" "<<curr.pa<<" "<<curr.pb<<" "<<curr.pw<<"\n";
        int x=curr.x;
        if(!par[x].empty() && par[x].back()[3]<=curr.len) continue ;
        
        par[x].push_back({curr.pa, curr.pb, curr.pw, curr.len});
        if(par[x].size()==1) {
            ans[x]=get_ans(x, 0);
        }
        
        for(auto i:adj[x]) {
            pq.push(state{i.xx, x, (int)par[x].size()-1, i.yy, curr.len+1});
        }
    }
    int sum=0;
    for(int i=2;i<=n;++i) {
        sum+=par[i].size();
        cout<<ans[i]<<"\n";
    }
    cerr<<double(sum)/n<<"\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2028 KiB
2Accepted26ms3192 KiB
subtask28/8
3Accepted9ms3036 KiB
4Accepted19ms2984 KiB
5Accepted12ms3108 KiB
6Accepted8ms3500 KiB
7Accepted52ms3504 KiB
subtask315/15
8Accepted3ms3388 KiB
9Accepted3ms3712 KiB
10Accepted3ms3672 KiB
11Accepted3ms3664 KiB
12Accepted3ms3672 KiB
13Accepted3ms4008 KiB
14Accepted3ms4148 KiB
15Accepted3ms4096 KiB
16Accepted3ms4356 KiB
17Accepted3ms4440 KiB
18Accepted3ms4384 KiB
subtask434/34
19Accepted16ms5204 KiB
20Accepted50ms5140 KiB
21Accepted57ms5320 KiB
22Accepted30ms5240 KiB
23Accepted29ms5284 KiB
24Accepted107ms4956 KiB
25Accepted12ms5512 KiB
26Accepted9ms5912 KiB
27Accepted98ms5236 KiB
28Accepted21ms5176 KiB
29Accepted26ms5200 KiB
30Accepted64ms5164 KiB
31Accepted54ms5300 KiB
32Accepted173ms5380 KiB
33Accepted177ms5576 KiB
subtask521/21
34Accepted4ms5060 KiB
35Accepted8ms5196 KiB
36Accepted7ms5388 KiB
37Accepted7ms5520 KiB
38Accepted6ms5492 KiB
39Accepted6ms5272 KiB
40Accepted70ms5144 KiB
41Accepted59ms5088 KiB
42Accepted4ms4908 KiB
43Accepted4ms4848 KiB
44Accepted4ms5144 KiB
45Accepted4ms5188 KiB
46Accepted4ms5060 KiB
47Accepted4ms5104 KiB
48Accepted4ms5076 KiB
49Accepted4ms5100 KiB
subtask622/22
50Accepted27ms5572 KiB
51Accepted25ms5796 KiB
52Accepted26ms6196 KiB
53Accepted21ms6120 KiB
54Accepted13ms6228 KiB
55Accepted13ms6360 KiB
56Accepted1.291s7484 KiB
57Accepted836ms7436 KiB
58Accepted8ms5628 KiB
59Accepted14ms5788 KiB
60Accepted17ms5784 KiB
61Accepted20ms5704 KiB
62Accepted20ms5692 KiB
63Accepted14ms5812 KiB
64Accepted71ms5764 KiB
65Accepted71ms5768 KiB
66Accepted223ms5780 KiB