#include <bits/stdc++.h>
using namespace std;
long long dist[300010], weight[300010], pre[300010];
vector<pair<long long, long long>> g[300010];
void solve() {
  long long n, m;
  cin >> n >> m;
  memset(dist, 63, sizeof dist);
  for (long long i = 1; i <= m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    g[a].push_back({b, i});
    g[b].push_back({a, i});
    weight[i] = c;
  }
  long long src;
  cin >> src;
  set<pair<long long, long long>> q;
  dist[src] = 0;
  q.insert({0, src});
  while (!q.empty()) {
    long long node = q.begin()->second;
    q.erase(q.begin());
    for (auto x : g[node]) {
      if (dist[x.first] > dist[node] + weight[x.second]) {
        q.erase({dist[x.first], x.first});
        dist[x.first] = dist[node] + weight[x.second];
        pre[x.first] = x.second;
        q.insert({dist[x.first], x.first});
      } else if (dist[x.first] == dist[node] + weight[x.second]) {
        if (weight[pre[x.first]] > weight[x.second]) pre[x.first] = x.second;
      }
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++)
    if (i != src) ans += weight[pre[i]];
  cout << ans << '\n';
  for (long long i = 1; i <= n; i++) {
    if (i != src) {
      cout << pre[i] << " ";
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long t = 1;
  while (t--) solve();
  return 0;
}
