#include <bits/stdc++.h>
using namespace std;
vector<long long> v[300009];
long long edges[3][300009], each_vertex[2][300009], taken[300009];
set<vector<long long> > st;
set<vector<long long> >::iterator it, erase_it;
int main() {
  long long t, n, m, a, b, u, weight, edge_idx, i, j, max_weight;
  max_weight = 1000000;
  max_weight *= max_weight;
  max_weight *= 1000;
  for (i = 0; i < 300009; ++i) v[i].clear();
  memset(edges, 0, sizeof(edges));
  memset(each_vertex, 0, sizeof(each_vertex));
  memset(taken, 0, sizeof(taken));
  st.clear();
  cin >> n >> m;
  for (i = 1; i <= m; ++i) {
    cin >> edges[0][i] >> edges[1][i] >> edges[2][i];
    v[edges[0][i]].push_back(i);
    v[edges[1][i]].push_back(i);
  }
  cin >> u;
  for (i = 1; i <= n; ++i) {
    vector<long long> temp;
    if (i == u)
      temp.push_back(0);
    else
      temp.push_back(max_weight);
    temp.push_back(0);
    temp.push_back(i);
    for (j = 0; j < 2; ++j) each_vertex[j][i] = temp[j];
    st.insert(temp);
  }
  while (!st.empty()) {
    it = st.begin();
    long long weight_till_now = (*it)[0];
    long long this_vertex = (*it)[2];
    taken[this_vertex] = 1;
    for (vector<long long>::iterator it_vector = v[this_vertex].begin();
         it_vector != v[this_vertex].end(); ++it_vector) {
      long long other_vertex =
          (this_vertex != edges[0][*it_vector] ? edges[0][*it_vector]
                                               : edges[1][*it_vector]);
      if (taken[other_vertex] == 0) {
        bool flag = false;
        if ((weight_till_now + edges[2][*it_vector]) <
            each_vertex[0][other_vertex]) {
          flag = true;
        } else if (((weight_till_now + edges[2][*it_vector]) ==
                    each_vertex[0][other_vertex]) &&
                   (edges[2][*it_vector] <
                    edges[2][each_vertex[1][other_vertex]])) {
          flag = true;
        }
        if (flag) {
          vector<long long> temp;
          temp.push_back(each_vertex[0][other_vertex]);
          temp.push_back(each_vertex[1][other_vertex]);
          temp.push_back(other_vertex);
          erase_it = st.find(temp);
          st.erase(erase_it);
          temp.clear();
          temp.push_back((weight_till_now + edges[2][*it_vector]));
          temp.push_back(*it_vector);
          temp.push_back(other_vertex);
          st.insert(temp);
          each_vertex[0][other_vertex] = weight_till_now + edges[2][*it_vector];
          each_vertex[1][other_vertex] = *it_vector;
        }
      }
    }
    st.erase(it);
  }
  long long sum = 0;
  for (i = 1; i <= n; ++i) {
    if (each_vertex[1][i] != 0) sum += edges[2][each_vertex[1][i]];
  }
  cout << sum << "\n";
  for (i = 1; i <= n; ++i) {
    if (each_vertex[1][i] != 0) cout << each_vertex[1][i] << " ";
  }
  return 0;
}
