#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 0x3f3f3f3f3f3f3f3fll;
int N, M, S;
struct T_Edge {
  int u, v, l;
  T_Edge() {}
  T_Edge(int u, int v, int l) : u(u), v(v), l(l) {}
  inline int other(int x) { return x == u ? v : u; }
} es[300009];
vector<int> tab[300009];
long long dis[300009];
int ans[300009];
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d%d%d", &es[i].u, &es[i].v, &es[i].l);
    tab[es[i].u].push_back(i);
    tab[es[i].v].push_back(i);
  }
  scanf("%d", &S);
  memset(dis, 0x3f, sizeof(dis));
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      q;
  q.push(make_pair(0ll, S));
  static bool used[300009];
  memset(used, 0, sizeof(used));
  memset(ans, -1, sizeof(ans));
  while (!q.empty()) {
    pair<long long, int> h = q.top();
    q.pop();
    int &u = h.second;
    long long &d = h.first;
    if (used[u]) continue;
    dis[u] = d;
    used[u] = true;
    for (size_t i = 0; i < tab[u].size(); i++) {
      T_Edge &e = es[tab[u][i]];
      int v = e.other(u);
      if (d + e.l <= dis[v] && (ans[v] == -1 || e.l < es[ans[v]].l))
        ans[v] = tab[u][i];
      if (!used[v] && d + e.l < dis[v]) {
        dis[v] = d + e.l;
        q.push(make_pair(d + e.l, v));
      }
    }
  }
  long long weight = 0;
  for (int i = 1; i <= N; i++)
    if (i != S) weight += es[ans[i]].l;
  printf("%I64d\n", weight);
  for (int i = 1; i <= N; i++)
    if (i != S) printf("%d ", ans[i] + 1);
  printf("\n");
  return 0;
}
