#include <bits/stdc++.h>
using namespace std;
struct Edge {
  Edge() {}
  Edge(int u, int v, int w) : u(u), v(v), w(w) {}
  int u, v, w;
};
vector<vector<int> > ve;
vector<Edge> edges;
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  ve.resize(n);
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    ve[u].push_back(i);
    ve[v].push_back(i);
    edges.push_back(Edge(u, v, w));
  }
  int io;
  cin >> io;
  --io;
  vector<long long> d(n, 999999999999999LL);
  vector<int> h(n, -1);
  vector<int> w(n, 999999999);
  d[io] = 0;
  w[io] = 0;
  priority_queue<pair<long long, int> > q;
  q.push(make_pair(0, io));
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    for (int i = 0; i < ve[u].size(); ++i) {
      Edge &e = edges[ve[u][i]];
      int v = (u == e.v ? e.u : e.v);
      if ((d[u] + e.w < d[v]) || (d[u] + e.w == d[v] && e.w < w[v])) {
        d[v] = d[u] + e.w;
        h[v] = ve[u][i] + 1;
        w[v] = e.w;
        q.push(make_pair(-d[v], v));
      }
    }
  }
  long long sum = 0;
  for (int i = 0; i < n; ++i) sum += w[i];
  cout << sum << "\n";
  for (int i = 0; i < n; ++i)
    if (i != io) cout << h[i] << " ";
  cout << "\n";
  return 0;
}
