#include <bits/stdc++.h>
using namespace std;
const long long int MAXN = 1e18 + 7;
const long long int MAXNN = 300005;
long long int int_pow(long long int base, long long int exp) {
  long long int result = 1;
  while (exp) {
    if (exp & 1) result *= base;
    exp /= 2;
    base *= base;
  }
  return result;
}
vector<long long int> g[MAXNN];
vector<long long int> w[MAXNN];
set<pair<long long int, long long int> > st;
long long int dist[MAXNN];
long long int par[MAXNN];
bool mark[MAXNN];
map<pair<long long int, long long int>, long long int> mpw;
map<pair<long long int, long long int>, long long int> mpn;
void diekstra(long long int n, long long int s) {
  fill_n(dist, MAXNN, MAXN);
  dist[s] = 0;
  for (long long int i = 1; i <= n; i++) st.insert({dist[i], i});
  while (!st.empty()) {
    set<pair<long long int, long long int> >::iterator it = st.begin();
    long long int x = it->second;
    mark[x] = 1;
    st.erase(it);
    for (long long int i = 0; i < (long long int)g[x].size(); i++) {
      long long int y = g[x][i];
      if (!mark[y]) {
        st.erase({dist[y], y});
        if (dist[y] >= dist[x] + w[x][i]) {
          par[y] = x;
        }
        dist[y] = min(dist[y], dist[x] + w[x][i]);
        st.insert({dist[y], y});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(par, -1, sizeof par);
  long long int n, m, u, v, z, stpoint = 0, ans = 0;
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> u >> v >> z;
    if (u > v) swap(u, v);
    mpn[{u, v}] = i + 1;
    mpw[{u, v}] = z;
    g[u].push_back(v);
    g[v].push_back(u);
    w[u].push_back(z);
    w[v].push_back(z);
  }
  cin >> stpoint;
  diekstra(n, stpoint);
  for (long long int i = 1; i <= n; i++) {
    if (i == stpoint) continue;
    u = i, v = par[i];
    if (u > v) swap(u, v);
    ans += mpw[{u, v}];
  }
  cout << ans << '\n';
  for (long long int i = 1; i <= n; i++) {
    if (i == stpoint) continue;
    u = i, v = par[i];
    if (u > v) swap(u, v);
    cout << mpn[{u, v}] << '\n';
  }
}
