#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
                                    !is_same<T_container, string>::value,
                                    typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
  os << '{';
  string sep;
  for (const T &x : v) os << sep << x, sep = ", ";
  return os << '}';
}
void dbg_out() { cerr << "\n"; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
vector<pair<long long, long long>> v[300001];
long long dist[300001];
long long par[300001];
map<pair<long long, long long>, long long> e;
map<pair<long long, long long>, long long> wt;
void dfs(long long node) {
  for (auto &c : dist) c = 1e18;
  dist[node] = 0;
  set<pair<long long, long long>> second;
  second.insert({0, node});
  while (!second.empty()) {
    long long vt = second.begin()->second;
    second.erase(second.begin());
    for (auto c : v[vt]) {
      if (dist[c.first] > dist[vt] + c.second ||
          (dist[c.first] == dist[vt] + c.second &&
           c.second < wt[{c.first, par[c.first]}])) {
        if (second.count({dist[c.first], c.first}))
          second.erase({dist[c.first], c.first});
        par[c.first] = vt;
        dist[c.first] = dist[vt] + c.second;
        second.insert({dist[c.first], c.first});
      }
    }
  }
}
void solve() {
  long long n, m;
  cin >> n >> m;
  long long second = 1;
  while (m--) {
    long long a, b, w;
    cin >> a >> b >> w;
    e[{a, b}] = e[{b, a}] = second;
    wt[{a, b}] = wt[{b, a}] = w;
    second++;
    v[a].push_back({b, w});
    v[b].push_back({a, w});
  }
  long long u;
  cin >> u;
  dfs(u);
  vector<long long> ans;
  long long tot = 0;
  for (long long i = 1; i <= n; i++) {
    if (i != u) {
      ans.push_back(e[{i, par[i]}]);
      tot += wt[{i, par[i]}];
    }
  }
  cout << tot << "\n";
  for (auto c : ans) cout << c << " ";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
