277 | 2021-06-22 15:25:14 | mraron | Vázsony vonatjegyet vásárol | cpp14 | Accepted 100/100 | 760ms | 171480 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});
}
queue<pair<int,ll>> dq;
vector<ll> dist(n+1, 1LL<<60);
dq.push({b,0});
dist[b]=0;
//spfa
while(!dq.empty()) {
pair<int,ll> akt=dq.front(); dq.pop();
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;
dq.push({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 | 6516 KiB | ||||
2 | Accepted | 127ms | 29104 KiB | ||||
subtask2 | 20/20 | ||||||
3 | Accepted | 52ms | 19140 KiB | ||||
4 | Accepted | 423ms | 72428 KiB | ||||
5 | Accepted | 298ms | 67524 KiB | ||||
6 | Accepted | 263ms | 67876 KiB | ||||
7 | Accepted | 330ms | 79068 KiB | ||||
8 | Accepted | 250ms | 76212 KiB | ||||
9 | Accepted | 601ms | 122712 KiB | ||||
subtask3 | 15/15 | ||||||
10 | Accepted | 164ms | 87604 KiB | ||||
11 | Accepted | 10ms | 57032 KiB | ||||
12 | Accepted | 10ms | 57508 KiB | ||||
13 | Accepted | 35ms | 63956 KiB | ||||
14 | Accepted | 68ms | 76960 KiB | ||||
subtask4 | 20/20 | ||||||
15 | Accepted | 63ms | 71656 KiB | ||||
16 | Accepted | 218ms | 93844 KiB | ||||
17 | Accepted | 52ms | 71912 KiB | ||||
18 | Accepted | 82ms | 82672 KiB | ||||
19 | Accepted | 35ms | 73576 KiB | ||||
20 | Accepted | 82ms | 81408 KiB | ||||
subtask5 | 45/45 | ||||||
21 | Accepted | 3ms | 64772 KiB | ||||
22 | Accepted | 760ms | 170524 KiB | ||||
23 | Accepted | 41ms | 85136 KiB | ||||
24 | Accepted | 237ms | 112696 KiB | ||||
25 | Accepted | 212ms | 113272 KiB | ||||
26 | Accepted | 333ms | 135872 KiB | ||||
27 | Accepted | 189ms | 123588 KiB | ||||
28 | Accepted | 600ms | 171480 KiB | ||||
29 | Accepted | 112ms | 119568 KiB | ||||
30 | Accepted | 509ms | 166172 KiB | ||||
31 | Accepted | 222ms | 138692 KiB | ||||
32 | Accepted | 41ms | 116192 KiB | ||||
33 | Accepted | 324ms | 150868 KiB | ||||
34 | Accepted | 286ms | 145932 KiB | ||||
35 | Accepted | 273ms | 138260 KiB | ||||
36 | Accepted | 291ms | 138312 KiB | ||||
37 | Accepted | 275ms | 138756 KiB |