#include <bits/stdc++.h>
using namespace std;
const long long inf = 100000000000000000;
const int MAXN = 300005;
const int MAXM = 300005;
struct edge {
  int v, w, pre;
} e[MAXM << 1];
struct node {
  int num;
  long long dis;
  bool operator<(const node &x) const { return dis > x.dis; }
};
priority_queue<node> Q;
int N, M, S, fst[MAXN];
long long dist[MAXN], f[MAXN], ans = 0;
int fr[MAXN], vis[MAXN];
int read() {
  int o = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9')
    o = (o << 3) + (o << 1) + (c & 15), c = getchar();
  return o;
}
void adde(int a, int b, int c, int k) {
  e[k] = (edge){b, c, fst[a]}, fst[a] = k;
  return;
}
void toposort(int k) {
  vis[k] = 1;
  for (int o = fst[k]; o; o = e[o].pre) {
    if (dist[k] + e[o].w == dist[e[o].v]) {
      if (f[e[o].v] < dist[k] || !vis[e[o].v]) {
        f[e[o].v] = dist[k], fr[e[o].v] = (o > M) ? o - M : o;
      }
      if (!vis[e[o].v]) toposort(e[o].v);
    }
  }
  return;
}
int main() {
  N = read(), M = read();
  for (int i = 1; i <= M; i++) {
    int a = read(), b = read(), c = read();
    adde(a, b, c, i), adde(b, a, c, i + M);
  }
  S = read();
  for (int i = 1; i <= N; i++) dist[i] = inf;
  dist[S] = 0;
  Q.push((node){S, 0});
  while (!Q.empty()) {
    node x = Q.top();
    Q.pop();
    if (x.dis <= dist[x.num]) {
      for (int o = fst[x.num]; o; o = e[o].pre) {
        if (dist[e[o].v] > x.dis + e[o].w) {
          dist[e[o].v] = x.dis + e[o].w;
          Q.push((node){e[o].v, dist[e[o].v]});
        }
      }
    }
  }
  toposort(S);
  for (int i = 1; i <= N; i++) ans += dist[i] - f[i];
  printf("%lld\n", ans);
  for (int i = 1; i <= N; i++)
    if (i != S) printf("%d ", fr[i]);
  return 0;
}
