12212022-03-26 20:45:15Valaki2Vázsony vonatjegyet vásárolcpp14Accepted 100/1001.105s209400 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second

struct edge {
    int from, to, weight;
    edge() : from(0), to(0), weight(0) {}
    edge(int from_, int to_, int weight_) : from(from_), to(to_), weight(weight_) {}
};

const int maxn = 1e5;
const int inf = 1e16;

int n, m, start, last;
vector<edge> graph[maxn + 1];
vector<edge> edges;
int dist[maxn + 1];
bool done[maxn + 1];

void dijkstra() {
    for(int i = 1; i <= n; i++) {
        dist[i] = inf;
    }
    priority_queue<pair<int, int> > q;
    q.push(mp(0, last));
    dist[last] = 0;
    while(!q.empty()) {
        pair<int, int> p = q.top();
        q.pop();
        int cur_dist = -p.fi, cur = p.se;
        if(done[p.se]) {
            continue;
        }
        done[cur] = true;
        for(edge nei : graph[cur]) {
            if(cur_dist + nei.weight < dist[nei.to]) {
                dist[nei.to] = cur_dist + nei.weight;
                q.push(mp(-(cur_dist + nei.weight), nei.to));
            }
        }
    }
}

int comp_cnt;
int comp[maxn + 1];
vector<int> by_comp[maxn + 1];
vector<int> graph_comps[maxn + 1];
int dp[maxn + 1];
int pre[maxn + 1];

void dfs(int cur) {
    comp[cur] = comp_cnt;
    by_comp[comp_cnt].pb(cur);
    for(edge nei : graph[cur]) {
        if((dist[nei.to] == dist[cur]) && !comp[nei.to]) {
            dfs(nei.to);
        }
    }
}

bool better(int a, int b) {
    return dist[by_comp[a][0]] < dist[by_comp[b][0]];
}

void solve() {
    cin >> n >> m >> start >> last;
    for(int i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        edges.pb(edge(a, b, c));
        graph[a].pb(edge(a, b, c));
        graph[b].pb(edge(b, a, c));
    }
    dijkstra();
    for(int i = 1; i <= n; i++) {
        if(!comp[i]) {
            comp_cnt++;
            dfs(i);
        }
    }
    for(edge e : edges) {
        if(dist[e.from] < dist[e.to]) {
            graph_comps[comp[e.to]].pb(comp[e.from]);
        }
        if(dist[e.from] > dist[e.to]) {
            graph_comps[comp[e.from]].pb(comp[e.to]);
        }
    }
    vector<int> comps(comp_cnt, 0);
    for(int i = 0; i < comp_cnt; i++) {
        comps[i] = i + 1;
    }
    sort(comps.begin(), comps.end(), better);
    for(int cur : comps) {
        for(int nei : graph_comps[cur]) {
            if(dp[cur] < dp[nei]) {
                dp[cur] = dp[nei];
                pre[cur] = nei;
            }
        }
        dp[cur] += (int) by_comp[cur].size();
    }
    cout << dp[comp[start]] << "\n";
    /*for(int i = 1; i <= n; i++) {
        cout << dist[i] << " ";
    }
    cout << "\n";
    for(int i = 1; i <= comp_cnt; i++) {
        cout << dp[i] << " " << pre[i] << " - ";
        for(int x : by_comp[i]) {
            cout << x << " ";
        }
        cout << "\n";
    }*/
    vector<int> ans_comps;
    int tmp = comp[start];
    ans_comps.pb(tmp);
    while(tmp != comp[last]) {
        tmp = pre[tmp];
        ans_comps.pb(tmp);
    }
    vector<int> ans;
    for(int x : ans_comps) {
        for(int y : by_comp[x]) {
            ans.pb(y);
        }
    }
    sort(ans.begin(), ans.end());
    for(int x : ans) {
        cout << x << " ";
    }
    cout << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted8ms15916 KiB
2Accepted171ms56340 KiB
subtask220/20
3Accepted71ms38532 KiB
4Accepted522ms135220 KiB
5Accepted414ms119160 KiB
6Accepted358ms105344 KiB
7Accepted414ms128788 KiB
8Accepted340ms114884 KiB
9Accepted833ms192540 KiB
subtask315/15
10Accepted208ms133804 KiB
11Accepted17ms66212 KiB
12Accepted17ms66920 KiB
13Accepted45ms80228 KiB
14Accepted92ms103308 KiB
subtask420/20
15Accepted93ms84992 KiB
16Accepted307ms101384 KiB
17Accepted63ms57652 KiB
18Accepted129ms72308 KiB
19Accepted61ms58192 KiB
20Accepted118ms69232 KiB
subtask545/45
21Accepted8ms45892 KiB
22Accepted1.105s209400 KiB
23Accepted59ms40708 KiB
24Accepted273ms92436 KiB
25Accepted233ms83620 KiB
26Accepted433ms117944 KiB
27Accepted254ms96856 KiB
28Accepted726ms200180 KiB
29Accepted153ms95328 KiB
30Accepted589ms196248 KiB
31Accepted273ms119304 KiB
32Accepted57ms93736 KiB
33Accepted370ms148588 KiB
34Accepted354ms154632 KiB
35Accepted368ms155364 KiB
36Accepted344ms157796 KiB
37Accepted342ms162304 KiB