5396 | 2023-05-09 14:12:52 | mraron | Autópálya infláció | cpp17 | Accepted 100/100 | 1.291s | 7484 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";
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2028 KiB | ||||
2 | Accepted | 26ms | 3192 KiB | ||||
subtask2 | 8/8 | ||||||
3 | Accepted | 9ms | 3036 KiB | ||||
4 | Accepted | 19ms | 2984 KiB | ||||
5 | Accepted | 12ms | 3108 KiB | ||||
6 | Accepted | 8ms | 3500 KiB | ||||
7 | Accepted | 52ms | 3504 KiB | ||||
subtask3 | 15/15 | ||||||
8 | Accepted | 3ms | 3388 KiB | ||||
9 | Accepted | 3ms | 3712 KiB | ||||
10 | Accepted | 3ms | 3672 KiB | ||||
11 | Accepted | 3ms | 3664 KiB | ||||
12 | Accepted | 3ms | 3672 KiB | ||||
13 | Accepted | 3ms | 4008 KiB | ||||
14 | Accepted | 3ms | 4148 KiB | ||||
15 | Accepted | 3ms | 4096 KiB | ||||
16 | Accepted | 3ms | 4356 KiB | ||||
17 | Accepted | 3ms | 4440 KiB | ||||
18 | Accepted | 3ms | 4384 KiB | ||||
subtask4 | 34/34 | ||||||
19 | Accepted | 16ms | 5204 KiB | ||||
20 | Accepted | 50ms | 5140 KiB | ||||
21 | Accepted | 57ms | 5320 KiB | ||||
22 | Accepted | 30ms | 5240 KiB | ||||
23 | Accepted | 29ms | 5284 KiB | ||||
24 | Accepted | 107ms | 4956 KiB | ||||
25 | Accepted | 12ms | 5512 KiB | ||||
26 | Accepted | 9ms | 5912 KiB | ||||
27 | Accepted | 98ms | 5236 KiB | ||||
28 | Accepted | 21ms | 5176 KiB | ||||
29 | Accepted | 26ms | 5200 KiB | ||||
30 | Accepted | 64ms | 5164 KiB | ||||
31 | Accepted | 54ms | 5300 KiB | ||||
32 | Accepted | 173ms | 5380 KiB | ||||
33 | Accepted | 177ms | 5576 KiB | ||||
subtask5 | 21/21 | ||||||
34 | Accepted | 4ms | 5060 KiB | ||||
35 | Accepted | 8ms | 5196 KiB | ||||
36 | Accepted | 7ms | 5388 KiB | ||||
37 | Accepted | 7ms | 5520 KiB | ||||
38 | Accepted | 6ms | 5492 KiB | ||||
39 | Accepted | 6ms | 5272 KiB | ||||
40 | Accepted | 70ms | 5144 KiB | ||||
41 | Accepted | 59ms | 5088 KiB | ||||
42 | Accepted | 4ms | 4908 KiB | ||||
43 | Accepted | 4ms | 4848 KiB | ||||
44 | Accepted | 4ms | 5144 KiB | ||||
45 | Accepted | 4ms | 5188 KiB | ||||
46 | Accepted | 4ms | 5060 KiB | ||||
47 | Accepted | 4ms | 5104 KiB | ||||
48 | Accepted | 4ms | 5076 KiB | ||||
49 | Accepted | 4ms | 5100 KiB | ||||
subtask6 | 22/22 | ||||||
50 | Accepted | 27ms | 5572 KiB | ||||
51 | Accepted | 25ms | 5796 KiB | ||||
52 | Accepted | 26ms | 6196 KiB | ||||
53 | Accepted | 21ms | 6120 KiB | ||||
54 | Accepted | 13ms | 6228 KiB | ||||
55 | Accepted | 13ms | 6360 KiB | ||||
56 | Accepted | 1.291s | 7484 KiB | ||||
57 | Accepted | 836ms | 7436 KiB | ||||
58 | Accepted | 8ms | 5628 KiB | ||||
59 | Accepted | 14ms | 5788 KiB | ||||
60 | Accepted | 17ms | 5784 KiB | ||||
61 | Accepted | 20ms | 5704 KiB | ||||
62 | Accepted | 20ms | 5692 KiB | ||||
63 | Accepted | 14ms | 5812 KiB | ||||
64 | Accepted | 71ms | 5764 KiB | ||||
65 | Accepted | 71ms | 5768 KiB | ||||
66 | Accepted | 223ms | 5780 KiB |