#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  long long f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= f;
}
void write(long long x) {
  if (x / 10) write(x / 10);
  putchar(x % 10 + 48);
}
void writeln(long long x) {
  write(x);
  putchar('\n');
}
const long long N = 6e5 + 5, M = 6e5 + 5;
long long tot = 1, nxt[M], to[M], h[N], fa[M], val[M];
void add(long long u, long long v, long long w) {
  to[++tot] = v, nxt[tot] = h[u], h[u] = tot, val[tot] = w;
}
struct info {
  long long x, dis;
};
bool operator<(info a, info b) { return a.dis > b.dis; }
priority_queue<info> q;
long long n, m;
long long d[N];
void dij(long long s) {
  long long ans = 0;
  memset(d, 0x3f3f3f3f3f3f3f3f, sizeof(d));
  d[s] = 0;
  q.push((info){s, 0});
  for (long long i = 1; i <= n; i++) {
    info tp = q.top();
    q.pop();
    while (!q.empty() && d[tp.x] != tp.dis) {
      tp = q.top();
      q.pop();
    }
    if (d[tp.x] != tp.dis) break;
    long long x = tp.x, dis = tp.dis;
    for (long long i = h[x]; i != -1; i = nxt[i]) {
      long long y = to[i];
      if (d[y] > d[x] + val[i]) {
        d[y] = d[x] + val[i];
        ans = ans - val[fa[y]] + val[i];
        fa[y] = i;
        q.push((info){y, d[y]});
      } else if (d[y] == d[x] + val[i] && val[i] < val[fa[y]]) {
        ans = ans - val[fa[y]] + val[i];
        fa[y] = i;
      }
    }
  }
  writeln(ans);
}
signed main() {
  memset(h, -1, sizeof(h));
  read(n), read(m);
  for (long long i = 1, u, v, w; i <= m; i++) {
    read(u), read(v), read(w);
    add(u, v, w), add(v, u, w);
  }
  long long s;
  read(s);
  dij(s);
  for (long long i = 1; i <= n; i++) {
    if (i == s) continue;
    write(fa[i] >> 1), putchar(' ');
  }
  puts("");
  return 0;
}
