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