#include <bits/stdc++.h>
using namespace std;
const long long maxn = 300005;
long long n, m, cnt, sta;
long long head[maxn], ans[maxn], dis[maxn];
bool vis[maxn];
struct edge {
  long long v, nxt, val;
} a[maxn << 1];
void add(long long x, long long y, long long val) {
  ++cnt;
  a[cnt].v = y;
  a[cnt].val = val;
  a[cnt].nxt = head[x];
  head[x] = cnt;
}
inline long long read() {
  long long ret = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (isdigit(ch)) {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret * f;
}
void scan() {
  n = read();
  m = read();
  cnt = 1;
  for (long long k = 1; k <= m; k++) {
    long long x, y, val;
    x = read();
    y = read();
    val = read();
    add(x, y, val);
    add(y, x, val);
  }
  sta = read();
}
void dijkstra() {
  long long sum = 0;
  memset(dis, 0x3f, sizeof(dis));
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      q;
  while (!q.empty()) q.pop();
  q.push(make_pair(dis[sta] = 0, sta));
  while (!q.empty()) {
    pair<long long, long long> t;
    long long x, d;
    do {
      t = q.top();
      q.pop();
      x = t.second;
      d = t.first;
    } while (vis[x] && !q.empty());
    if (vis[x]) break;
    vis[x] = 1;
    sum += a[ans[x] << 1].val;
    for (long long k = head[x]; k; k = a[k].nxt) {
      long long v = a[k].v, val = a[k].val;
      if (d + val <= dis[v]) ans[v] = k >> 1;
      if (d + val < dis[v]) q.push(make_pair(dis[v] = d + val, v));
    }
  }
  printf("%lld\n", sum);
  for (long long k = 1; k <= n; k++)
    if (k != sta) printf("%lld ", ans[k]);
  puts("");
}
signed main() {
  scan();
  dijkstra();
  return 0;
}
