#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const long long SIZE = 500005;
long long nxt[SIZE << 1], ver[SIZE << 1], head[SIZE << 1], tot, val[SIZE << 1];
inline void add(long long x, long long y, long long z) {
  ver[++tot] = y, val[tot] = z, nxt[tot] = head[x], head[x] = tot;
}
struct node {
  long long id, dst;
  bool operator<(const node &x) const { return dst > x.dst; }
};
priority_queue<node> q;
long long n, m, d[SIZE], t, pre[SIZE], ans;
bool v[SIZE];
void dijkstra(long long s) {
  memset(d, 0x7f, sizeof(d));
  d[s] = 0, q.push((node){s, 0});
  while (!q.empty()) {
    node tmp = q.top();
    q.pop();
    long long x = tmp.id;
    if (v[x]) continue;
    v[x] = true;
    for (long long i = head[x]; i; i = nxt[i]) {
      long long y = ver[i];
      if (d[y] > d[x] + val[i]) pre[y] = i, d[y] = d[x] + val[i];
      q.push((node){y, d[y]});
      if (d[y] == d[x] + val[i] && val[pre[y]] > val[i]) pre[y] = i;
    }
  }
}
int main() {
  n = read(), m = read();
  for (long long i = 1; i <= m; i++) {
    long long x = read(), y = read(), z = read();
    add(x, y, z), add(y, x, z);
  }
  t = read(), dijkstra(t);
  for (long long i = 1; i <= n; i++) ans += val[pre[i]];
  for (long long i = 1; i <= n; i++) d[i] = (pre[i] + 1) >> 1;
  sort(d + 1, d + n + 1);
  printf("%lld\n", ans);
  for (long long i = 2; i <= n; i++) printf("%lld ", d[i]);
  return 0;
}
