#include <bits/stdc++.h>
long long MOD = int(1e9) + 7;
int debug = 1;
using namespace std;
int mk[300100];
int n, m;
vector<vector<pair<int, int> > > g;
map<pair<int, int>, int> d, cn;
int main() {
  cin >> n >> m;
  g.resize(n + 1);
  int a, b, c;
  int op = 0;
  while (m--) {
    op++;
    cin >> a >> b >> c;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
    d[{a, b}] = op;
    d[{b, a}] = op;
    if (a > b) swap(a, b);
    cn[{a, b}] = c;
  }
  long long dist[n + 3];
  int src;
  cin >> src;
  for (int i = 0; i < n + 2; i++) {
    dist[i] = 100000000000000000LL;
  }
  priority_queue<pair<pair<long long, int>, pair<int, int> >,
                 vector<pair<pair<long long, int>, pair<int, int> > >,
                 greater<pair<pair<long long, int>, pair<int, int> > > >
      q;
  dist[src] = 0;
  q.push(make_pair(make_pair(0LL, -1), make_pair(-1, src)));
  vector<int> v;
  long long ans = 0;
  int oi, ol;
  while (!q.empty()) {
    pair<pair<long long, int>, pair<int, int> > p = q.top();
    q.pop();
    long long dis = p.first.first;
    int last_edge = p.first.second;
    int last_vertex = p.second.first;
    int vertex = p.second.second;
    if (dis > dist[vertex] || mk[vertex] == 1) continue;
    mk[vertex] = 1;
    oi = min(vertex, last_vertex), ol = max(vertex, last_vertex);
    ans += cn[{oi, ol}];
    v.push_back(d[make_pair(vertex, last_vertex)]);
    for (int i = 0; i < g[vertex].size(); i++) {
      pair<int, int> pr = g[vertex][i];
      int wt = pr.second;
      int v = pr.first;
      long long next_dist = dis + wt;
      if (next_dist <= dist[v]) {
        dist[v] = next_dist;
        q.push(make_pair(make_pair(next_dist, wt), make_pair(vertex, v)));
      }
    }
  }
  cout << ans << endl;
  for (int i = 1; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  return 0;
}
