#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long INF = 2000000000000000000;
long long s;
int vika[300010];
map<pair<long long, long long>, pair<long long, long long> > mmm;
int main() {
  srand(time(NULL));
  cin >> n >> m;
  vector<long long> d(n + 1, INF), p(n + 1);
  vector<vector<pair<long long, long long> > > g(n + 1);
  for (long long i = 1; i <= m; ++i) {
    long long a, b, c;
    cin >> a >> b >> c;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
    mmm[make_pair(a, b)] = make_pair(i, c);
    mmm[make_pair(b, a)] = make_pair(i, c);
  }
  cin >> s;
  d[s] = 0;
  set<pair<long long, long long> > q;
  q.insert(make_pair(d[s], s));
  while (!q.empty()) {
    long long v = q.begin()->second;
    q.erase(q.begin());
    for (size_t j = 0; j < g[v].size(); ++j) {
      long long to = g[v][j].first;
      long long llen = g[v][j].second;
      if (d[v] + llen < d[to] || d[v] + llen == d[to] && llen < vika[to]) {
        q.erase(make_pair(d[to], to));
        d[to] = d[v] + llen;
        p[to] = v;
        vika[to] = llen;
        q.insert(make_pair(d[to], to));
      }
    }
  }
  long long ss = 0;
  vector<int> ans;
  for (long long i = 1; i <= n; ++i) {
    if (i == s) continue;
    long long pr = p[i];
    pair<long long, long long> par = mmm[make_pair(i, pr)];
    ans.push_back(par.first);
    ss += par.second;
  }
  cout << ss << endl;
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << ' ';
  }
}
