#include <bits/stdc++.h>
using namespace std;
const int N = 300001;
vector<pair<int, int> > v[N];
long long dist[N];
int pre[N];
int weight[N];
int n, m;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) dist[i] = 1e18;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    v[b].push_back(make_pair(a, i));
    v[a].push_back(make_pair(b, i));
    weight[i] = c;
  }
  int source;
  cin >> source;
  priority_queue<pair<long long, int> > q;
  q.push(make_pair(0LL, source));
  dist[source] = 0LL;
  pre[source] = 0;
  weight[0] = 0;
  while (!q.empty()) {
    int node = q.top().second;
    q.pop();
    int x = v[node].size();
    while (x--) {
      int next = v[node][x].first;
      int id = v[node][x].second;
      int weigh = weight[id];
      if (dist[next] > dist[node] + weigh) {
        dist[next] = dist[node] + weigh;
        pre[next] = id;
        q.push(make_pair(-dist[next], next));
      } else {
        if (dist[next] == dist[node] + weigh) {
          if (weigh < weight[pre[next]]) {
            pre[next] = id;
          }
        }
      }
    }
  }
  long long sum = 0;
  for (int i = 1; i <= n; i++) sum += weight[pre[i]];
  cout << sum << "\n";
  for (int i = 1; i <= n; i++) {
    if (i != source) {
      cout << pre[i] << " ";
    }
  }
  return 0;
}
