#include <bits/stdc++.h>
using namespace std;
vector<vector<tuple<int, int, int>>> g;
void dijkestra(long long s) {
  vector<long long> dist(g.size(), numeric_limits<long long>::max());
  vector<bool> vis(g.size(), false);
  dist[s] = 0;
  long long ans = 0;
  priority_queue<tuple<long long, int, int, int>,
                 vector<tuple<long long, int, int, int>>,
                 greater<tuple<long long, int, int, int>>>
      Q;
  Q.push(make_tuple(0ll, 0, s, 0));
  int nbr = 0;
  vector<int> reponse;
  while (nbr < g.size()) {
    tuple<long long, int, int, int> p = Q.top();
    Q.pop();
    long long w = get<0>(p);
    int u = get<2>(p);
    if (vis[u]) continue;
    vis[u] = true;
    reponse.push_back(get<3>(p));
    ans += get<1>(p);
    nbr++;
    int l = g[u].size();
    for (int i = 0; i < l; i++) {
      int v = get<1>(g[u][i]);
      if (!vis[v] && w + get<0>(g[u][i]) <= dist[v]) {
        dist[v] = w + get<0>(g[u][i]);
        Q.push(make_tuple(dist[v], get<0>(g[u][i]), v, get<2>(g[u][i])));
      }
    }
  }
  cout << ans << endl;
  int l = reponse.size();
  for (int i = 1; i < l; i++) {
    cout << reponse[i] << " ";
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  g.resize(n);
  int cpt = 0;
  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    g[a].push_back(make_tuple(c, b, ++cpt));
    g[b].push_back(make_tuple(c, a, cpt));
  }
  int s;
  scanf("%d", &s);
  dijkestra(--s);
  return 0;
}
