#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& stream, const vector<T> v) {
  stream << "[ ";
  for (int i = 0; i < (int)v.size(); i++) stream << v[i] << " ";
  stream << "]";
  return stream;
}
long long fpow(long long x, long long p, long long m) {
  long long r = 1;
  for (; p; p >>= 1) {
    if (p & 1) r = r * x % m;
    x = x * x % m;
  }
  return r;
}
long long inv(long long a, long long b) {
  return a < 2 ? a : ((a - inv(b % a, a)) * b + 1) / a % b;
}
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
struct edge {
  int id, to;
  edge(int id, int to) : id(id), to(to) {}
};
int N, M, c[300010], deg[300010];
vector<edge> g[300010], gd[300010];
long long dist[300010];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> M;
  int i, x, y;
  for (i = 1; i <= M; i++) {
    cin >> x >> y >> c[i];
    g[x].push_back(edge(i, y));
    g[y].push_back(edge(i, x));
  }
  int u;
  cin >> u;
  for (i = 1; i <= N; i++) dist[i] = -1;
  dist[u] = 0;
  set<pair<int, int> > st;
  st.insert({0, u});
  while (!st.empty()) {
    pair<int, int> cr = *st.begin();
    st.erase(st.begin());
    int nod = cr.second;
    for (edge e : g[nod]) {
      long long d = c[e.id] + dist[nod];
      if (dist[e.to] == -1 || d < dist[e.to]) {
        if (dist[e.to] != -1) st.erase({dist[e.to], e.to});
        dist[e.to] = d;
        st.insert({d, e.to});
      }
    }
  }
  for (i = 1; i <= N; i++)
    for (edge e : g[i])
      if (dist[i] + c[e.id] == dist[e.to])
        gd[e.to].push_back({e.id, i}), deg[i]++;
  vector<int> q;
  for (i = 1; i <= N; i++)
    if (deg[i] == 0) q.push_back(i);
  vector<int> ans;
  long long res = 0;
  for (i = 0; i < q.size(); i++) {
    int nod = q[i];
    if (nod == u) break;
    long long minc = (1ll << 40), id = 0;
    for (edge e : gd[nod]) {
      deg[e.to]--;
      if (c[e.id] < minc) {
        minc = c[e.id];
        id = e.id;
      }
      if (deg[e.to] == 0) q.push_back(e.to);
    }
    ans.push_back(id);
    res += minc;
  }
  cout << res << "\n";
  for (i = 0; i < N - 1; i++) cout << ans[i] << " ";
  return 0;
}
