#include <bits/stdc++.h>
using namespace std;
template <typename T, typename V>
inline void bugp(const pair<T, V> &x) {
  cerr << '{' << x.first << ", " << x.second << '}' << endl;
}
template <typename T, typename U, typename V>
inline void bugpp(const pair<T, pair<U, V> > &x) {
  cerr << '{' << x.first << ", {" << x.second.first << ", " << x.second.second
       << "}}" << endl;
}
const int N = 300003;
int n;
long long d[N], ans = 0;
vector<int> res;
pair<int, int> pi[N];
vector<pair<int, pair<int, int> > > G[N];
void dijkstra(int src) {
  fill(d + 1, d + n + 1, 1e15);
  fill(pi + 1, pi + n + 1, pair<int, int>(1e9 + 7, 0));
  d[src] = 0;
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      pq;
  pq.push({0, src});
  int u, x, id, w;
  long long du;
  while (pq.size()) {
    u = pq.top().second;
    du = pq.top().first;
    pq.pop();
    if (du != d[u]) continue;
    if (u != src) {
      ans += pi[u].first;
      res.push_back(pi[u].second);
    }
    for (pair<int, pair<int, int> > &v : G[u]) {
      id = v.first;
      w = v.second.first;
      x = v.second.second;
      if (d[x] > du + w) {
        pi[x] = {w, id};
        pq.push({d[x] = du + w, x});
      } else if (d[x] == du + w && w < pi[x].first)
        pi[x] = {w, id};
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int m, u, v, w;
  cin >> n >> m;
  for (int i = 1, _n = (m); i <= _n; ++i) {
    cin >> u >> v >> w;
    G[u].push_back({i, {w, v}});
    G[v].push_back({i, {w, u}});
  }
  cin >> u;
  dijkstra(u);
  cout << ans << '\n';
  for (int i = 0, _n = (res.size()); i < _n; ++i) cout << res[i] << ' ';
  return 0;
}
