#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
  return (b) == 0 ? (a) : gcd((b), ((a) % (b)));
}
template <class T>
inline T lcm(T a, T b) {
  return ((a) / gcd((a), (b)) * (b));
}
template <class T>
inline T BigMod(T Base, T power, T M = 1000000007) {
  if (power == 0) return 1;
  if (power & 1)
    return ((Base % M) * (BigMod(Base, power - 1, M) % M)) % M;
  else {
    T y = BigMod(Base, power / 2, M) % M;
    return (y * y) % M;
  }
}
template <class T>
inline T ModInv(T A, T M = 1000000007) {
  return BigMod(A, M - 2, M);
}
int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0};
int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0};
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n, m, k, start;
vector<pair<long long, pair<int, int> > > v[300005];
vector<int> edge;
long long ans, dis[300005], pk[300005];
int vis[300005];
void dij() {
  for (int i = 1; i <= n; i++) dis[i] = 1e17;
  dis[start] = 0;
  priority_queue<pair<pair<long long, long long>, pair<int, int> >,
                 vector<pair<pair<long long, long long>, pair<int, int> > >,
                 greater<pair<pair<long long, long long>, pair<int, int> > > >
      pq;
  pq.push(pair<pair<long long, long long>, pair<int, int> >(
      pair<long long, long long>(0, 0), pair<int, int>(start, -1)));
  while (!pq.empty()) {
    pair<pair<long long, long long>, pair<int, int> > x = pq.top();
    pq.pop();
    if (vis[x.second.first]) continue;
    vis[x.second.first] = 1;
    if (x.second.second != -1) edge.push_back(x.second.second);
    ans += x.first.second;
    for (auto it : v[x.second.first]) {
      if (vis[it.second.first] == 0 &&
          dis[it.second.first] >= dis[x.second.first] + it.first) {
        dis[it.second.first] = dis[x.second.first] + it.first;
        pq.push(pair<pair<long long, long long>, pair<int, int> >(
            pair<long long, long long>(dis[it.second.first], it.first),
            pair<int, int>(it.second.first, it.second.second)));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    pk[i + 1] = c;
    v[a].push_back(
        pair<long long, pair<int, int> >(c, pair<int, int>(b, i + 1)));
    v[b].push_back(
        pair<long long, pair<int, int> >(c, pair<int, int>(a, i + 1)));
  }
  cin >> start;
  dij();
  cout << ans << endl;
  for (auto it : edge) cout << it << " ";
  return 0;
}
