281 | 2021-06-22 15:28:14 | mraron | Vázsony vonatjegyet vásárol | cpp14 | Accepted 100/100 | 769ms | 176416 KiB |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define xx first
#define yy second
struct el {
ll to, w;
};
vector<el> adj[100001];
struct dsu {
vector<int> par;
vector<int> sz;
dsu(int n) : par(n,-1) {sz.assign(n,1);}
int get(int x) {
if(par[x]==-1) return x;
return par[x]=get(par[x]);
}
void merge(int x, int y) {
int px=get(x), py=get(y);
if(px==py) return ;
if(sz[px]>sz[py]) {
swap(px,py);
swap(x,y); //:) lyft
}
sz[py]+=sz[px];
par[px]=py;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m,a,b;
cin>>n>>m>>a>>b;
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});
}
deque<pair<int,ll>> dq;
vector<ll> dist(n+1, 1LL<<60);
dq.push_back({b,0});
dist[b]=0;
//optimized spfa
while(!dq.empty()) {
pair<int,ll> akt=dq.front(); dq.pop_front();
if(akt.yy>dist[akt.xx]) continue ;
for(auto i:adj[akt.first]) {
if(dist[i.to]>akt.yy+i.w) {
dist[i.to]=akt.yy+i.w;
if(!dq.empty() && dq.front().yy>dist[i.to])
dq.push_front({i.to, dist[i.to]});
else
dq.push_back({i.to, dist[i.to]});
}
}
}
dsu d(n+1);
for(int i=1;i<=n;++i) {
for(auto j:adj[i]) {
if(dist[i]==dist[j.to]) d.merge(i, j.to);
}
}
vector<vector<int>> lst(n+1, vector<int>());
for(int i=1;i<=n;++i) {
lst[d.get(i)].push_back(i);
}
vector<int> dp(n+1), par(n+1, -1);
dp[b]=1;
vector<int> ord;
for(int i=1;i<=n;++i) ord.push_back(i);
sort(ord.begin(), ord.end(), [&](int i, int j) -> bool {return dist[i]<dist[j];});
for(int i:ord) {
for(auto j:adj[i]) {
if(dist[d.get(j.to)]<dist[d.get(i)]) {
int ii=d.get(i), jj=d.get(j.to);
if(dp[ii]<dp[jj]+d.sz[ii]) {
dp[ii]=dp[jj]+d.sz[ii];
par[ii]=jj;
}
}
}
}
int akt=d.get(a);
vector<int> ans={b};
while(akt!=b) {
for(auto i:lst[akt]) ans.push_back(i);
akt=par[akt];
}
cout<<ans.size()<<"\n";
for(auto i:ans) cout<<i<<" ";
cout<<"\n";
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 4ms | 6488 KiB | ||||
2 | Accepted | 118ms | 29264 KiB | ||||
subtask2 | 20/20 | ||||||
3 | Accepted | 52ms | 19132 KiB | ||||
4 | Accepted | 400ms | 72796 KiB | ||||
5 | Accepted | 293ms | 67476 KiB | ||||
6 | Accepted | 241ms | 67912 KiB | ||||
7 | Accepted | 294ms | 79240 KiB | ||||
8 | Accepted | 259ms | 76244 KiB | ||||
9 | Accepted | 583ms | 124252 KiB | ||||
subtask3 | 15/15 | ||||||
10 | Accepted | 164ms | 90916 KiB | ||||
11 | Accepted | 9ms | 60228 KiB | ||||
12 | Accepted | 12ms | 60724 KiB | ||||
13 | Accepted | 37ms | 67168 KiB | ||||
14 | Accepted | 75ms | 80176 KiB | ||||
subtask4 | 20/20 | ||||||
15 | Accepted | 63ms | 74872 KiB | ||||
16 | Accepted | 196ms | 97068 KiB | ||||
17 | Accepted | 39ms | 75148 KiB | ||||
18 | Accepted | 75ms | 85884 KiB | ||||
19 | Accepted | 37ms | 76788 KiB | ||||
20 | Accepted | 82ms | 84564 KiB | ||||
subtask5 | 45/45 | ||||||
21 | Accepted | 3ms | 67984 KiB | ||||
22 | Accepted | 769ms | 176416 KiB | ||||
23 | Accepted | 43ms | 90844 KiB | ||||
24 | Accepted | 243ms | 119120 KiB | ||||
25 | Accepted | 199ms | 119088 KiB | ||||
26 | Accepted | 342ms | 141616 KiB | ||||
27 | Accepted | 208ms | 126936 KiB | ||||
28 | Accepted | 606ms | 171652 KiB | ||||
29 | Accepted | 115ms | 116368 KiB | ||||
30 | Accepted | 522ms | 162432 KiB | ||||
31 | Accepted | 284ms | 132280 KiB | ||||
32 | Accepted | 45ms | 98224 KiB | ||||
33 | Accepted | 374ms | 136984 KiB | ||||
34 | Accepted | 286ms | 125600 KiB | ||||
35 | Accepted | 273ms | 125568 KiB | ||||
36 | Accepted | 261ms | 129108 KiB | ||||
37 | Accepted | 259ms | 132148 KiB |