2812021-06-22 15:28:14mraronVázsony vonatjegyet vásárolcpp14Accepted 100/100769ms176416 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;
    
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6488 KiB
2Accepted118ms29264 KiB
subtask220/20
3Accepted52ms19132 KiB
4Accepted400ms72796 KiB
5Accepted293ms67476 KiB
6Accepted241ms67912 KiB
7Accepted294ms79240 KiB
8Accepted259ms76244 KiB
9Accepted583ms124252 KiB
subtask315/15
10Accepted164ms90916 KiB
11Accepted9ms60228 KiB
12Accepted12ms60724 KiB
13Accepted37ms67168 KiB
14Accepted75ms80176 KiB
subtask420/20
15Accepted63ms74872 KiB
16Accepted196ms97068 KiB
17Accepted39ms75148 KiB
18Accepted75ms85884 KiB
19Accepted37ms76788 KiB
20Accepted82ms84564 KiB
subtask545/45
21Accepted3ms67984 KiB
22Accepted769ms176416 KiB
23Accepted43ms90844 KiB
24Accepted243ms119120 KiB
25Accepted199ms119088 KiB
26Accepted342ms141616 KiB
27Accepted208ms126936 KiB
28Accepted606ms171652 KiB
29Accepted115ms116368 KiB
30Accepted522ms162432 KiB
31Accepted284ms132280 KiB
32Accepted45ms98224 KiB
33Accepted374ms136984 KiB
34Accepted286ms125600 KiB
35Accepted273ms125568 KiB
36Accepted261ms129108 KiB
37Accepted259ms132148 KiB