#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 7;
const long long MOD = 1e9 + 7;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H) << "\n";
  debug_out(T...);
}
const int N = 3e5 + 8;
vector<tuple<long long, long long, long long>> g[N];
long long dist[N];
int n, m;
void dijkstra(int u) {
  for (int i = 1; i <= n; i++) {
    dist[i] = INF;
  }
  dist[u] = 0;
  set<pair<long long, long long>> st;
  for (int i = 1; i <= n; i++) {
    st.insert({dist[i], i});
  }
  while (!st.empty()) {
    auto it = st.begin();
    long long d = it->first;
    int v = it->second;
    st.erase(it);
    for (auto edge : g[v]) {
      int child = get<0>(edge);
      long long w = get<1>(edge);
      if (dist[child] > d + w) {
        st.erase(st.find({dist[child], child}));
        dist[child] = d + w;
        st.insert({dist[child], child});
      }
    }
  }
}
void solve() {
  cin >> n >> m;
  vector<long long> weights(m + 1);
  for (int i = 1; i <= m; i++) {
    int u, v;
    long long w;
    cin >> u >> v >> w;
    g[u].emplace_back(v, w, i);
    g[v].emplace_back(u, w, i);
    weights[i] = w;
  }
  int u;
  cin >> u;
  dijkstra(u);
  vector<int> indegree(n + 1, 0);
  vector<multiset<pair<long long, long long>>> incoming(n + 1);
  vector<bool> taken(m + 1, false);
  for (int i = 1; i <= n; i++) {
    for (auto e : g[i]) {
      int c = get<0>(e);
      long long w = get<1>(e);
      int id = get<2>(e);
      if (dist[c] == dist[i] + w) {
        incoming[c].insert({weights[id], id});
        indegree[c]++;
        taken[id] = 1;
      }
    }
  }
  priority_queue<pair<int, int>> pq;
  for (int i = 1; i <= n; i++) {
    pq.push(make_pair(indegree[i], i));
  }
  vector<bool> removed(m + 1, false);
  for (int i = 1; i <= m; i++) {
    if (taken[i] == 1) continue;
    removed[i] = 1;
  }
  while (!pq.empty()) {
    auto tp = pq.top();
    int indeg = tp.first;
    int ver = tp.second;
    if (indeg <= 1) {
      break;
    }
    indegree[ver]--;
    auto it = prev(incoming[ver].end());
    removed[it->second] = 1;
    incoming[ver].erase(it);
    pq.pop();
    pq.emplace(indegree[ver], ver);
  }
  long long minimum_weight = 0;
  vector<int> ans;
  for (int i = 1; i <= m; i++) {
    if (removed[i] == 1) continue;
    minimum_weight += weights[i];
    ans.push_back(i);
  }
  cout << minimum_weight << endl;
  for (int i = 0; i < int(ans.size()); i++) {
    if (i > 0) cout << ' ';
    cout << ans[i];
  }
  cout << '\n';
}
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  for (int tc = 1; tc <= tt; tc++) {
    solve();
  }
}
