2772021-06-22 15:25:14mraronVázsony vonatjegyet vásárolcpp14Accepted 100/100760ms171480 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;
    
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6516 KiB
2Accepted127ms29104 KiB
subtask220/20
3Accepted52ms19140 KiB
4Accepted423ms72428 KiB
5Accepted298ms67524 KiB
6Accepted263ms67876 KiB
7Accepted330ms79068 KiB
8Accepted250ms76212 KiB
9Accepted601ms122712 KiB
subtask315/15
10Accepted164ms87604 KiB
11Accepted10ms57032 KiB
12Accepted10ms57508 KiB
13Accepted35ms63956 KiB
14Accepted68ms76960 KiB
subtask420/20
15Accepted63ms71656 KiB
16Accepted218ms93844 KiB
17Accepted52ms71912 KiB
18Accepted82ms82672 KiB
19Accepted35ms73576 KiB
20Accepted82ms81408 KiB
subtask545/45
21Accepted3ms64772 KiB
22Accepted760ms170524 KiB
23Accepted41ms85136 KiB
24Accepted237ms112696 KiB
25Accepted212ms113272 KiB
26Accepted333ms135872 KiB
27Accepted189ms123588 KiB
28Accepted600ms171480 KiB
29Accepted112ms119568 KiB
30Accepted509ms166172 KiB
31Accepted222ms138692 KiB
32Accepted41ms116192 KiB
33Accepted324ms150868 KiB
34Accepted286ms145932 KiB
35Accepted273ms138260 KiB
36Accepted291ms138312 KiB
37Accepted275ms138756 KiB