#include <bits/stdc++.h>
using namespace std;
const long long INF = 4000000000000000;
int main() {
  int N;
  scanf("%d", &N);
  vector<vector<pair<long long, long long> > > edges(N);
  int M;
  scanf("%d", &M);
  map<pair<long long, long long>, int> index;
  for (int j = 1; j < M + 1; j++) {
    int first, second, dist;
    scanf("%d%d%d", &first, &second, &dist);
    edges[first - 1].push_back(make_pair(dist, second - 1));
    edges[second - 1].push_back(make_pair(dist, first - 1));
    index[pair<long long, long long>(first - 1, second - 1)] = j;
    index[pair<long long, long long>(second - 1, first - 1)] = j;
  }
  int s;
  scanf("%d", &s);
  s--;
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      Q;
  vector<long long> dist(N, INF), dad(N, -1);
  Q.push(make_pair(0, s));
  dist[s] = 0;
  while (!Q.empty()) {
    pair<long long, long long> p = Q.top();
    Q.pop();
    int here = p.second;
    for (vector<pair<long long, long long> >::iterator it = edges[here].begin();
         it != edges[here].end(); it++) {
      if (dist[here] + it->first < dist[it->second]) {
        dist[it->second] = dist[here] + it->first;
        dad[it->second] = here;
        Q.push(make_pair(dist[it->second], it->second));
      }
    }
  }
  vector<pair<long long, long long> > goodedges[N];
  for (int i = 0; i < N; i++) {
    for (pair<long long, long long> j : edges[i]) {
      if (dist[i] + j.first == dist[j.second]) {
        goodedges[j.second].push_back(make_pair(j.first, i));
      }
    }
  }
  long long res = 0;
  vector<int> ans;
  for (int i = 0; i < N; i++) {
    if (i == s) continue;
    sort(goodedges[i].begin(), goodedges[i].end());
    res += goodedges[i][0].first;
    ans.push_back(index[pair<long long, long long>(i, goodedges[i][0].second)]);
  }
  printf("%I64d \n", res);
  for (int i : ans) {
    printf("%d ", i);
  }
  cout << "\n";
  return 0;
}
