#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000 + 333;
struct edge {
  int to, w, id;
  edge(int to, int w, int id) : to(to), w(w), id(id) {}
  edge() {}
};
long long dist[MAXN];
vector<edge> g[MAXN];
set<pair<int, int> > s;
int n, m, start, par_e[MAXN], par_w[MAXN];
void load() {
  scanf("%d %d", &n, &m);
  for (int i = 0, u, v, w; i < m; i++) {
    scanf("%d %d %d", &u, &v, &w);
    g[u].push_back(edge(v, w, i + 1));
    g[v].push_back(edge(u, w, i + 1));
  }
  scanf("%d", &start);
  fill(dist, dist + n + 1, LLONG_MAX);
  fill(par_e, par_e + n + 1, -1);
}
void solve() {
  dist[start] = 0ll;
  s.insert({0, start});
  while (s.size()) {
    int v = (*s.begin()).second;
    s.erase(s.begin());
    for (edge e : g[v]) {
      if (dist[e.to] > dist[v] + e.w) {
        dist[e.to] = dist[v] + e.w;
        par_w[e.to] = e.w;
        par_e[e.to] = e.id;
        s.insert({dist[e.to], e.to});
      } else if (dist[e.to] == dist[v] + e.w && e.w < par_w[e.to]) {
        par_e[e.to] = e.id;
        par_w[e.to] = e.w;
      }
    }
  }
  long long answer = 0ll;
  for (int i = 1; i <= n; i++) answer += par_w[i];
  cout << answer << endl;
  for (int i = 1; i <= n; i++)
    if (i != start) printf("%d ", par_e[i]);
}
int main() {
  load();
  solve();
  return 0;
}
