#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n, m, u, v;
  long long w, d;
  cin >> n >> m;
  vector<vector<pair<pair<int, long long>, int> > > G(n + 1);
  for (int i = 0; i < m; ++i) {
    cin >> u >> v >> w;
    G[u].push_back(make_pair(make_pair(v, w), i + 1));
    G[v].push_back(make_pair(make_pair(u, w), i + 1));
  }
  cin >> u;
  priority_queue<pair<long long, pair<int, pair<int, int> > > > Q;
  Q.push(make_pair(0, make_pair(0, make_pair(u, -1))));
  vector<long long> D(n + 1, -1);
  vector<long long> S(n + 1, 0x3fffffffffffffff);
  S[u] = 0;
  int trees = n, edge;
  vector<int> ans;
  long long total = 0;
  while (!Q.empty() && trees > 0) {
    d = -Q.top().first;
    w = -Q.top().second.first;
    u = Q.top().second.second.first;
    edge = Q.top().second.second.second;
    Q.pop();
    if (D[u] == -1) {
      --trees;
      total += w;
      if (edge != -1) ans.push_back(edge);
      D[u] = d;
      for (int i = 0; i < G[u].size(); ++i) {
        v = G[u][i].first.first, w = G[u][i].first.second;
        if (d + w <= S[v]) {
          S[v] = d + w;
          Q.push(make_pair(-S[v], make_pair(-w, make_pair(v, G[u][i].second))));
        }
      }
    }
  }
  cout << total << '\n';
  for (int i = 0; i < ans.size(); ++i) {
    if (i) cout << ' ';
    cout << ans[i];
  }
}
