#include <bits/stdc++.h>
const int maxn = 1200010;
int n, m, mk[maxn], eu[maxn], t[maxn];
int next[maxn], nbs[maxn], ev[maxn], f[maxn], ew[maxn];
long long d[maxn];
int open[maxn], open2[maxn];
const long long inf = 0x7fffffffffffffLL;
void spfa(int x) {
  open[1] = x;
  for (int i = 1; i <= n; i++) d[i] = inf, f[i] = -1;
  d[x] = 0, f[x] = 0;
  memset(mk, 0, sizeof(mk));
  int tail2, tail = 1;
  int u, v, p = 0;
  while (p++, tail) {
    for (int i = 1; i <= tail; i++) open2[i] = open[i];
    tail2 = tail;
    tail = 0;
    for (int i = 1; i <= tail2; i++) {
      u = open2[i];
      for (int j = nbs[u]; j != -1; j = next[j]) {
        v = ev[j];
        if (d[u] + ew[j] < d[v] || (d[u] + ew[j] == d[v] && ew[j] < ew[f[v]])) {
          d[v] = d[u] + ew[j];
          f[v] = j;
          if (mk[v] != p) {
            mk[v] = p;
            open[++tail] = v;
          }
        }
      }
    }
  }
}
int main() {
  while (scanf("%d %d", &n, &m) != EOF) {
    int num = 0;
    memset(nbs, 0xff, sizeof(nbs));
    for (int i = 1; i <= m; i++) {
      int u, v, w;
      scanf("%d %d %d", &u, &v, &w);
      next[num] = nbs[u];
      nbs[u] = num;
      eu[num] = u;
      ev[num] = v;
      ew[num] = w;
      num++;
      next[num] = nbs[v];
      nbs[v] = num;
      eu[num] = v;
      ev[num] = u;
      ew[num] = w;
      num++;
    }
    int r;
    scanf("%d", &r);
    spfa(r);
    long long ret = 0;
    for (int i = 1; i <= n; i++)
      if (i != r) ret += ew[f[i]];
    printf("%I64d\n", ret);
    for (int i = 1; i <= n; i++)
      if (i != r) printf("%d ", (f[i] >> 1) + 1);
    puts("");
  }
  return 0;
}
