12212022-03-26 20:45:15Valaki2Vázsony vonatjegyet vásárolcpp14Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva8ms15916 KiB
2Elfogadva171ms56340 KiB
subtask220/20
3Elfogadva71ms38532 KiB
4Elfogadva522ms135220 KiB
5Elfogadva414ms119160 KiB
6Elfogadva358ms105344 KiB
7Elfogadva414ms128788 KiB
8Elfogadva340ms114884 KiB
9Elfogadva833ms192540 KiB
subtask315/15
10Elfogadva208ms133804 KiB
11Elfogadva17ms66212 KiB
12Elfogadva17ms66920 KiB
13Elfogadva45ms80228 KiB
14Elfogadva92ms103308 KiB
subtask420/20
15Elfogadva93ms84992 KiB
16Elfogadva307ms101384 KiB
17Elfogadva63ms57652 KiB
18Elfogadva129ms72308 KiB
19Elfogadva61ms58192 KiB
20Elfogadva118ms69232 KiB
subtask545/45
21Elfogadva8ms45892 KiB
22Elfogadva1.105s209400 KiB
23Elfogadva59ms40708 KiB
24Elfogadva273ms92436 KiB
25Elfogadva233ms83620 KiB
26Elfogadva433ms117944 KiB
27Elfogadva254ms96856 KiB
28Elfogadva726ms200180 KiB
29Elfogadva153ms95328 KiB
30Elfogadva589ms196248 KiB
31Elfogadva273ms119304 KiB
32Elfogadva57ms93736 KiB
33Elfogadva370ms148588 KiB
34Elfogadva354ms154632 KiB
35Elfogadva368ms155364 KiB
36Elfogadva344ms157796 KiB
37Elfogadva342ms162304 KiB